multithreading - C# Form closing all threads join -
i have simple piece of code:
private volaile bool working; private volatile list<thread> threads = new list<thread>(); private volatile form face; public void start(int n) { working = true; (int = 0; <= n; i++) { thread worker = new thread(() => { while(working) { // work } }); threads.add(worker); worker.start(); } } public void stop() { if(working) { working = false; logger.info("waiting threads join"); foreach (thread worker in threads) { worker.join(); } logger.info("threads joined"); } } private void face_closing(object sender, system.componentmodel.canceleventargs e) { face.invoke(new action(() => {stop();})); system.environment.exit(0); }
face form creates on programm start , have controls, when use start() , stop() methods, works fine (all threads join normally).
but when press "x" form button, programm stacks on "waiting threads join". why? missing?
thread sample = new thread(new threadstart(thread1)); sample.isbackground= true;
try set threads background threads.
Comments
Post a Comment