multithreading - thread start stop c# WinForm -
background: in c# winform, utilize several threads this
private thread thread1; thread1 = new thread(new threadstart(dosomething)); thread1.start(); and want set timer stop/kill thread1 every hour, , restart new thread1 this:
abort/kill thread1; thread1 = new thread(new threadstart(dosomething)); thread1.start(); so how kill thread1 , restart new 1 without restart winform?
thank kindly reply. much appreciated it
here sample.
private void button1_click(object sender, eventargs e) { var = 0; action dosomething = () => { while (true) { (++i).tostring(); thread.sleep(100); } }; thread thread1; thread1 = new thread(new threadstart(dosomething)); thread1.start(); thread.sleep(1000); text = i.tostring(); thread1.abort(); thread1 = new thread(new threadstart(dosomething)); thread1.start(); } i don't recommend thread.abort method. when possible, design thread can stop safety. , utilize bring together method.
private void button2_click(object sender, eventargs e) { // flag, stop thread outside. var needstop = false; var = 0; action dosomething = () => { while (!needstop) { (++i).tostring(); thread.sleep(100); } }; thread thread1; thread1 = new thread(new threadstart(dosomething)); thread1.start(); thread.sleep(1000); text = i.tostring(); // alter flag stop. needstop = true; // wait until thread stop. thread1.join(); // start new thread. thread1 = new thread(new threadstart(dosomething)); thread1.start(); // needstop = true; // thread1.join(); } c# multithreading winforms
No comments:
Post a Comment