First time , i write an application that is running a foreground(defult) thread :
t = new Thread(new ThreadStart(GenerateRandom));
t.Start();
And when i close my form while running thread , it crashes , and throw an exception . after some search (How to close all running threads?), i founded that a foreground thread keeps running after closing main and i have to set my thread to background , so i do somthing like this :
t = new Thread(new ThreadStart(GenerateRandom));
t.IsBackground = true;
t.Start();
but it still throwing exception and i cant close mhy form while running my thread !!!
And i don't khow WHY ?!
I tried some solutions in FormClosing event :
- Use :
Enviroment.Exit(Enviroment.ExitCode);
- Use :
thread.Abort();
First one doesen't worked , and Second on one crashes when i close form befor running thread .
here is some pictures of my program :
Picture 1 , Befor closing form :
Picture 2 , after push the Close Button :
Thank you all .