2017-08-18 1 views
0

私はwinformアプリケーションでnancyfxを使用する際に問題があります(私はwinformアプリケーションを作成し、アプリケーション内でnancyfxを使用します)。これにより、追加のサーバーやサービスを使わずにwinformを変更できるAPI URLを使用できますWinフォームアプリケーションでのナンシー)ここで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); 
       } 
      }; 
    } 
} 
} 

であり、これは

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(); 
    } 
} 

私HostingAPI.csであり、それが成功し、エラーなしで実行されますが、私は、APIを呼び出すとき(localho st:5005/Start)winform appsのテキストボックスには、私が望むテキスト( "Api Worked")は追加されません。 APIコールがあるときにNancyfxが別のスレッドを作成し、invoke/begininvokeを使用できることに気付きました!なぜなら!invokerequiredは常に値がfalseであるためです。では、どうすればメインスレッドにアクセスするか、APIを呼び出すときにUIを更新する別のソリューションにアクセスできます。

ありがとうございました

答えて

1

ここには2つの問題があります。

  1. あなたは見えない別のForm1のインスタンスを作成し、あなたにそのクラス

  2. クロススレッドの問題内のアクセス特定のメソッドを実行しようとナンシー・モジュール内で、その後をForm1インスタンスからホストAPIサービスを開始あなたは正当に推測した。 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

ベアーイェーイ、それが動作します。私はシングルトンの形について考えることさえしません。どうもありがとう。 –

関連する問題