multithreading - Threads C# application crash -
hi guys i'm having problems c# , threads, i'm new this, i'm doing wrong?
public system.windows.forms.textbox text_box; public static thread object_t = new thread (); public static thread worker_thread = new thread (object_t.execute_t); public imagine_test () { initializecomponent (); this.text = "title"; } private void initializecomponent () { this.text_box = new system.windows.forms.textbox (); this.suspendlayout (); this.text_box.name = "text_box"; this.text_box.location = new system.drawing.point (5, 5); this.text_box.multiline = true; this.text_box.height = 50; this.text_box.width = 500; this.text_box.borderstyle = 0; this.clientsize = new system.drawing.size (510, 60); this.controls.addrange (new system.windows.forms.control[] { this.text_box }); load_func (); this.resumelayout (false); } public static void main () { application.run (new imagine_test ()); } public void load_func () { text_box.appendtext ("lorem"); worker_thread.start (); while (!worker_thread.isalive); thread.sleep (1); object_t.opreste_fir1 (); worker_thread.join (); } } public class thread : imagine_test { public void execute_t () { while (!_shouldstop) { //thread.sleep(5000); // run code text_box.appendtext ("some text"); } } public void opreste_fir1 () { _shouldstop = true; } private volatile bool _shouldstop;
you can't access control's members different thread 1 created it.
this line cause exception:
text_box.appendtext ("some text");
you need marshal call user interface thread:
text_box.invoke(new action( () => text_box.appendtext("some text") ) );
Comments
Post a Comment