如何在Nancyfx开始的工作线程中运行主线程? C#

时间:2017-08-18 10:23:17

标签: c# .net multithreading winforms nancy

我在winform应用程序中使用nancyfx时遇到问题(我在应用程序中创建了一个winform应用程序并使用了nancyfx)所以我可以使用一些API网址在winform中进行更改而无需额外的服务器或服务(因为我附加了) winform应用程序中的nancy)

这是我的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(localhost:5005 / Start)时,winform应用程序中的文本框不会添加我想要的文本(“Api Worked”)。我注意到这是因为Nancyfx在有API调用时会创建另一个线程,并且我可以使用invoke / begininvoke因为!invokerequired总是带有值false。那么当我调用API时,如何访问主线程或者更新UI的其他解决方案。

由于

1 个答案:

答案 0 :(得分:1)

这里有2个问题。

  1. 您从 Form1 实例启动主机api服务,然后在Nancy Module中创建一个不可见的Form1实例,并尝试访问该类中的某些方法

    < / LI>
  2. 您正确猜到的跨线程问题。您正在尝试从另一个线程上下文写入而不是UI线程

  3. 请查看下面的代码来实现此目的。请记住,您可以创建Singleton Form或查找另一种方法来访问 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
                {
                }
            }
        }
    
相关问题