0

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 :

  1. Use : Enviroment.Exit(Enviroment.ExitCode);
  2. 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 : Befor Closing Form

Picture 2 , after push the Close Button : After Closing Form

Thank you all .

Community
  • 1
  • 1
Parsa
  • 7,995
  • 2
  • 27
  • 37
  • 1
    in your screenshot error is conversion error it is because your text value is string you can call Trim on testbox value – MMM Jun 26 '16 at 08:24
  • 1
    Checking before aborting comes to mind: if (t!= null && t.IsAlive) t.Abort(); – TaW Jun 26 '16 at 08:29
  • 3
    The little `CheckForIllegalCrossThreadCalls = false` in your constructor. What could possibly go wrong? Oh wait, **this** could go wrong – Kevin Gosse Jun 26 '16 at 08:51

2 Answers2

1

Did you see the exception message FormatException. Would have you search with that name you would have got the idea. It has no relation with your running thread. It's occurring cause, the entered text is not a number and thus trying to parse it as Int failing with that exception.

Rahul
  • 76,197
  • 13
  • 71
  • 125
  • 2
    I think this error occurs because UI exited before terminating thread ... is this wrong ? – Parsa Jun 26 '16 at 08:54
  • 1
    @Parsa, NO, as stated in answer it has no relation with UI tread exited or not. You should check what you are inputting in your `textbox3`. it's definitely not a number and so the exception. – Rahul Jun 26 '16 at 08:56
  • 2
    But when i choose little numbers like 100000 it crashes too ! – Parsa Jun 26 '16 at 09:06
1

Usually you run your background job UNTIL the form is closed.

Something like:

void myThreadJob()
{
   while (!IsDisposed)
   {
      // do my "infinite" task until the form is disposed
   }
}

Another method is having a boolean field in your form. When your form is closed, set the field to true in FormClosing event. You then check the boolean field status in the while loop in thread code.

  • 2
    it throw exception again ! An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll Additional information: Value Dispose() cannot be called while doing CreateHandle(). – Parsa Jun 26 '16 at 09:43
  • 1
    Ok. Then try to use the 2nd approach (boolean field, and set its value in form closing to stop the thread) –  Jun 26 '16 at 09:46