Monday, May 31, 2010

TargetInvocationException in BackgroundWorker class

To avoid TargetInvocationException exception in BackgroundWorker class, in BackgroundWorker.RunWorkerCompleted event handler don't access RunWorkerCompletedEventArgs.Result property in case of user cancellation or exception.

void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (!e.Cancelled && (null == e.Error))
    {
        this.labelResult.Text = e.Result as String;
    }
}

1 comment: