I have a UserControl with something that does some background work. When thing has a completed event, which the user control is subscribed to. Since the event is raised in a separate thread and the event handler does some updating of the user control I start the event handler with this:
private void ProcessProcessCompleted(object sender, ProcessCompletedEventArgs e)
{
if (InvokeRequired)
{
Invoke(new Action<object, ProcessCompletedEventArgs>(ProcessProcessCompleted),
new[] { sender, e });
return;
}
// Update the user control, etc.
}
This works fine, except if the form that contains the user control is closed before the process has completed. I tried to unsubscribe to the event in the dispose method of the user control, but that only seems to work parts of the time. How should I be doing this? Would like to avoid wrapping it in a try catch block. Is there an InvokeIsPossible
property or something I can check before I Invoke?