2017-08-18 60 views
0

我在我的winform應用程序中使用nancyfx時出現了問題(我製作了一個winform應用程序並在應用程序中使用了nancyfx)因此,我可以使用一些API url來更改winform而無需附加服務器或服務(因爲我附加南希在WinForm應用程序)如何在由Nancyfx啓動的工作線程在主線程中運行? C#

這裏是我的Form1.cs的

public partial class Form1 : Form 
{ 

    public Form1(bool test) 
    { 
     InitializeComponent(); 

     textBox1.Text += "Apps Method "+ Environment.NewLine; 

    } 

    public bool startTestAPI() 
    { 
     textBox1.Text += "Api Worked" + Environment.NewLine); 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     HostingAPI s = new HostingAPI(); 
     s.Start(); 
     textBox1.Text += "Api Running" + Environment.NewLine); 
    } 
} 


public class ModuleCDM : NancyModule 
{ 

    public ModuleCDM() 
    { 
     try 
     { 
      Thread th2 = Thread.CurrentThread; 
      Get["/Start"] = parameters => 
      { 

       Form1 form = new Form1(false); 
       Thread testthread = Form1.curthread; 

       bool res = form.startTestAPI(); 

       if (res == true) 
       { 
        var feeds = new string[] { "Success" }; 
        return Response.AsJson(feeds); 
       } 
       else 
       { 
        var feeds = new string[] { "Failed" }; 
        return Response.AsJson(feeds); 
       } 
      }; 
    } 
} 
} 

,這是我HostingAPI.cs

public class HostingAPI 
{ 
    private NancyHost hostNancy; 

    private string hostUrl; 

    public void Start() 
    { 
     hostUrl = ConfigModule.ModuleAddress; 

     if (hostUrl == null) hostUrl = "http://localhost:5005"; 

     hostNancy = new NancyHost(new Uri(hostUrl)); 

     hostNancy.Start(); 

    } 

    public void Stop() 
    { 
     hostNancy.Stop(); 
    } 
} 

而且它沒有錯誤成功運行,但是當我調用API (localho st:5005/Start)winform應用中的文本框不會添加我想要的文本(「Api Worked」)。我注意到這是因爲Nancyfx在有API調用時創建另一個線程,並且我可以使用invoke/begininvoke,因爲!invokerequired總是帶有值false。那麼我怎樣才能訪問主線程或者可能是另一個解決方案來在我調用API時更新UI。

謝謝

回答

1

您有2問題在這裏。

  1. 您啓動主機API服務從Form1中實例,然後內南希模塊創建一個不同的Form1的實例,它是無形的,你嘗試該類

  2. 跨線程問題,因爲內做訪問某些方法你理所當然地猜到了。您正試圖從另一個線程上下文編寫UI線程

請看下面的代碼來實現此目的。請記住,您可以創建辛格爾頓形式或者找到另一種方式來訪問的實例Form1中

public class HostingAPI 

    { 
     private NancyHost hostNancy; 

     private string hostUrl; 

     public HostingAPI() 
     { 
     } 

     public void Start() 
     { 
      var hostConfig = new HostConfiguration 
      { 
       UrlReservations = new UrlReservations 
       { 
        CreateAutomatically = true 
       }, 
      }; 

      //hostUrl = ConfigModule.ModuleAddress; 

      if (hostUrl == null) hostUrl = "http://localhost:5005"; 

      hostNancy = new NancyHost(hostConfig,new Uri(hostUrl)); 

      hostNancy.Start(); 

     } 

     public void Stop() 
     { 
      hostNancy.Stop(); 
     } 
    } 

public partial class Form1 : Form 
{ 
    delegate void SetTextCallback(string text); 
    public static Form1 Instance; 
    public Form1(bool test) 
    { 
     InitializeComponent(); 

     textBox1.Text += "Apps Method " + Environment.NewLine; 
     Instance = this; 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     HostingAPI s = new HostingAPI(); 
     s.Start(); 
     textBox1.Text += "Api Running" + Environment.NewLine; 
    } 
    public void startTestAPI() 
    { 
     SetText("Api Worked" + Environment.NewLine); 
    } 

    private void SetText(string text) 
    { 
     if (this.textBox1.InvokeRequired) 
     { 
      SetTextCallback d = new SetTextCallback(SetText); 
      this.Invoke(d, new object[] { text }); 
     } 
     else 
     { 
      this.textBox1.Text += text; 
     } 
    } 
} 


public class ModuleCDM : NancyModule 
    { 
     public ModuleCDM() 
     { 
      try 
      { 
       Thread th2 = Thread.CurrentThread; 
       Get["/Start"] = parameters => 
       { 
        var form1 = Form1.Instance; 
        form1.startTestAPI(); 
        var feeds = new[] {"Success"}; 
        return Response.AsJson(feeds); 
       }; 
      } 
      catch 
      { 
      } 
     } 
    } 
+0

耶它的工作原理。我甚至不考慮單身形式。非常感謝。 –

相關問題