I have a problem with 2 Forms in a Form application. (Form1 and Form2)

Form1 is my starting Form and Form2 is the Form I open from Form1.

In Form2 I will execute a lot of code using backGroundWorkers and everything works fine here as long as this Form2 is open.

Now comes the problem when I close this Form2 on the X in the upper right corner, sometimes but not everytime(70 %), Form1 get completely stuck and the program does not respond and I need to close the application in TaskManager.

I dont know why this is happening. I start with asking this question as there is a lot of code and I dont know what is causing this.

But my mainquestion here though is, if Form2 isn´t closed to 100% and there is code that "gets stuck" somewhere.
Is there any code that could be executed when closing this Form2 that really will discard the Form2 in anyway ?
I know the question might be a bit diffus.

However I have 5 backgroundWorker that I Cancel like this when closing the form if they can be the problem but it doesn´t help anyway.

private: System::Void Form2_FormClosed(System::Object^  sender, System::Windows::Forms::FormClosedEventArgs^  e) 
{
		 backgroundWorker1->CancelAsync();
		 backgroundWorker2->CancelAsync();
		 backgroundWorker3->CancelAsync();
		 backgroundWorker4->CancelAsync();
		 backgroundWorker5->CancelAsync();
}

Try commenting out the worker threads and see if that fixes the problem. If not, then you know something else is wrong. If the problem goes away then try uncommenting worker threads one at a time until the program is broke again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.