Form.Close() hangs when invoked from thread

Hi,

I have a windows form that starts another thread. When the thread's work is done, I want the thread to go ahead and close the form. To make it thread safe, what I did was the following:

//This is a delegate

privatedelegatevoidCloseFormSafely();

...

//This is in the thread that's defind in the form

this.CloseSafely();

}

...

//also in the form

privatevoid CloseSafely()

{

if (this.InvokeRequired)

{

CloseFormSafely d =newCloseFormSafely(CloseSafely);

this.Invoke(d);

}

else

{

this.Close();

}

}

While this works all fine in VS.Net 2003, it for some reason doesn't work in VS.Net 2005 when I run in debug mode. The execution stops and freaz at "this.Close();". I have to stop debugging to get out. Am I missing something here?

Please help!

Thanks!

Feng

[2354 byte] By [Feng26] at [2007-12-24]
# 1
This could possibly be a problem with the IDE debugger. Does it work okay in release mode?
nobugz at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2
For InvokeRequired to work there has to be a valid handle for the control, if there is none it will return false. There is a possibilty that your thread finishes before there is a valid handle ... I would take out the check and just do an Invoke from the thread upon completion to see if that handles the close properly.
OmegaMan at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms General...