M
Mehdi
Hi,
If you want to do some operations on a UI control from a thread other than
the UI thread, you've got to first marshall the call to the UI thread using
Control.Invoke or Control.BeginInvoke. So i've created in my application a
UI control solely for the purpose of marchalling operations to the UI
thread and i'm using code like that to do the marshalling:
private void NotifyStatusChanged(Status newStatus)
{
if (m_uiControl.InvokeRequired)
{
// Marshall call to UI thread
m_uiControl.BeginInvoke(new
NotifyStatusChangedDelegate(NotifyStatusChanged), new object[]
{newStatus});
}
else
{
// We are in the UI thread now
// Do whatever we need to do
}
}
This works fine while the application is running.
When i close the application, the above method is called one last time
before everything stops and that's where it blows up: an
InvalidOperationException "Cannot call Invoke or InvokeAsync on a control
until the window handle has been created" is thrown when BeginInvoke is
called. I know that the control's handle has been created since i've
created it myself and it worked fine for the whole duration of the
application anyway. The IsHandleCreated property of my control returns true
when the exception is thrown. Its IsDisposed and Disposing properties both
return false.
So does anybody know why i get this exception and how i could check before
calling Invoke or BeginInvoke that my control is in a state suitable for
this operation? I could of course wrap the call around a try/catch block
and swallow the exception but I'd prefer to understand what the real
problem is.
Thanks.
If you want to do some operations on a UI control from a thread other than
the UI thread, you've got to first marshall the call to the UI thread using
Control.Invoke or Control.BeginInvoke. So i've created in my application a
UI control solely for the purpose of marchalling operations to the UI
thread and i'm using code like that to do the marshalling:
private void NotifyStatusChanged(Status newStatus)
{
if (m_uiControl.InvokeRequired)
{
// Marshall call to UI thread
m_uiControl.BeginInvoke(new
NotifyStatusChangedDelegate(NotifyStatusChanged), new object[]
{newStatus});
}
else
{
// We are in the UI thread now
// Do whatever we need to do
}
}
This works fine while the application is running.
When i close the application, the above method is called one last time
before everything stops and that's where it blows up: an
InvalidOperationException "Cannot call Invoke or InvokeAsync on a control
until the window handle has been created" is thrown when BeginInvoke is
called. I know that the control's handle has been created since i've
created it myself and it worked fine for the whole duration of the
application anyway. The IsHandleCreated property of my control returns true
when the exception is thrown. Its IsDisposed and Disposing properties both
return false.
So does anybody know why i get this exception and how i could check before
calling Invoke or BeginInvoke that my control is in a state suitable for
this operation? I could of course wrap the call around a try/catch block
and swallow the exception but I'd prefer to understand what the real
problem is.
Thanks.