winforms - C# calling form.show() from another thread -
winforms - C# calling form.show() from another thread -
if phone call form.show()
on winforms object thread, form throw exception. way can add together new, visible form main app thread? otherwise, how can open form without stopping executing thread?
here sample code. attempting start thread , execute work within thread. work progresses, show form.
public void main() { new thread(new threadstart(showform)).start(); // rest of main thread goes here... } public void showform() { // work here. myform form = new myform(); form.text = "my text"; form.show(); // more work here }
try using invoke call:
public static form globalform; void main() { globalform = new form(); globalform.show(); globalform.hide(); // spawn threads here } void threadproc() { myform form = new myform(); globalform.invoke((methodinvoker)delegate() { form.text = "my text"; form.show(); }); }
the "invoke" phone call tells form "please execute code in thread rather mine." can create changes winforms ui within delegate.
more documentation invoke here: http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx
edit: need utilize winforms object exists in order phone call invoke. i've shown here how can create global object; otherwise, if have other windows objects, work well.
c# winforms multithreading .net-3.5
Comments
Post a Comment