1

I've been trying to follow this MSDN example, and using the code below. However, e.Error is ALWAYS null in RunWorkerCompleted even when an error does occur in SomeMethod();

private void WorkerDoWork(object sender, DoWorkEventArgs e)
{
   getMethod = SomeMethod();
}

private void Worker_RunWorkerCompleted(object sender,
                                             RunWorkerCompletedEventArgs e)
{
    if (e.Error != null)
    {
        var result = ModernDialog.ShowMessage("Error occurred.... " +
                                   e.Result, "ErrorTitle", MessageBoxButton.OK);
    }
    else if (e.Cancelled)
    {

    }
    Else
    {

    }
}

Can anyone see what I'm doing wrong?

I can get around it by doing the following but I don't really understand why the example in MSDN is not working for me?

private void WorkerDoWork(object sender, DoWorkEventArgs e)
{
    try
    {
        getMethod = SomeMethod();
    }
    catch(Exception ex)
    {
        e.Result = ex;
    }
}

private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{

    if (e.Result is Exception)
    {
            var result = ModernDialog.ShowMessage("Error occurred.... " + e.Result, "ErrorTitle", MessageBoxButton.OK);
    }

    //etc
}

Also, using the second method I can't access the .Message from e.Result. For example, in WorkerDoWork I can use ex.Message

Edit: I've setup the worker to create it's own error and I still get e.Error == null. The variable displayed is a bit faint as CTRL+PrtSc makes it fade enter image description here

  • In WinForms, I cannot duplicate the issue. Are you sure you aren't catching the error somewhere? – LarsTech Nov 08 '13 at 21:03
  • Definitely not catching the error, SomeMethod(); contains no try,catch statements. It's in a WPF app if that makes a difference? –  Nov 08 '13 at 21:05
  • Take a look at [my question](http://stackoverflow.com/questions/19793377/servicecontroler-getservices-silently-bailing-in-a-background-thread), which might be relivent. – gunr2171 Nov 08 '13 at 21:14
  • 1
    Can't duplicate in WPF either. I always get `e.Error != null` when I throw an exception in the DoWork method. – LarsTech Nov 08 '13 at 21:15
  • What about if throwing the error in SomeMethod()? –  Nov 08 '13 at 21:24
  • No difference. You might have to show what you are doing in your SomeMethod procedure. – LarsTech Nov 08 '13 at 21:28
  • I'm getting really strange behaviour then. I'm removed the method and fixed the worker to raise an error. I still get e.Error == null??? WTF. See update image –  Nov 08 '13 at 21:34

1 Answers1

3

I think the problem is your empty exception block in emailWorkerDoWork(). For the result to be an exception you can't catch the exceptions in your background worker.

So something like this should give you the desired result:

private void emailWorkerDoWork(object sender, DoWorkEventArgs e)
{
    int value = 1 / int.Parse("0");
}

I found another SO answer that confirms my suspicion and provides a MSFT reference here.

Community
  • 1
  • 1
Sven Grosen
  • 5,616
  • 3
  • 30
  • 52