In my application I have a user control that do async operations using thread pool. The thread pool method looks like:
private void AsyncFunction(object state)
{
... do the calculation
//refresh the grid data on the UI thread
this.BeginInvoke(new MethodInvoker(() =>
{
... update the ui
}));
}
My problem is that if the user closes the dialog ... the user control gets disposed and I get the exception:
Invoke or BeginInvoke cannot be called on a control until the window handle has been created.
Do you know a way to detect if the dialog was disposed? I don't want to hae a property on control that the dialog set when closed. Is there another way of solving this?
Thanks,
Radu
Control.IsDisposed
You can use Control.IsDisposed
property.
try
{
if(!this.IsDisposed)
{
this.BeginInvoke(new MethodInvoker(() =>
{
// update my control
}
));
}
}
catch ( InvalidOperationException )
{
// Do something meaningful if you need to.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With