.net - How to run in main thread from worker thread that start by Nancyfx? C# -
i have problem using nancyfx in winform application (i make winform app , use nancyfx inside application) can use api url make change in winform without additional server or services (because attached nancy in winform apps)
here 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); } }; } } }
and 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(); } }
and run without error, when call api (localhost:5005/start) textbox in winform apps not add text wanted ("api worked"). noticed because nancyfx create thread when there api call, , can use invoke/begininvoke because !invokerequired comes value false. how can access main thread or maybe solution update ui when call api.
thanks
you have 2 issues in here.
you start host api service form1 instance within nancy module create different form1 instance invisible , try access methods within class
cross thread issue rightfully guessed . trying write thread context ui thread
look @ code @ below achieve this. bear in mind can create singleton form or find way access instance of 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 { } } }
Comments
Post a Comment