Let's assume that inside a Windows Form, I start a long-running task like this:
ThreadPool.QueueUserWorkItem(new WaitCallback(UpdateDailyTasksChart));
Then, this function runs for a while, but I close the window before it finishes.
private void UpdateDailyTasksChart (object param)
{
//Call Data Access Layer, go get MySQL Data. But I close the Form.
UICallback(chartData); //The Form is closed when this is executed.
}
And now here's what UICallback does:
private delegate void UICallbackDel(object chartData);
private void UICallback (object chartData)
{
if (InvokeRequired)
{
this.Invoke(new UICallbackDel(UICallback), chartData);
}
else
{
aButtonOnMyForm.Visible = false; //But the Form has been closed!
}
}
Curiously, this code doesn't crash.
I placed breakpoints in the Form_Closed event and it does execute. I haven't checked if the Form still exists by, for example, declaring it with a class variable. But my guess is that it does.
So the question is: the GC will only collect the Form when my thread finishes? Or what does it happen?