How does the BackgroundWorker e.error work?

1

I'm creating an application that makes multiple connections simultaneously. For each connection I create a TextBox and a BackgroundWorker . When I run DoWork of BackgroundWorker I use several ProgressPercentage that initiate various types of functionality, from starting a timer to putting messages in TextBox .

My question is, when I do the routine in DoWork I left some exceptions as throw new NotImplementedException(); and implemented in RunWorkerCompleted a treatment for when this type of error happened. However, when I am debugging, I end up stopping the creation of the error and not the treatment within RunWorkerCompleted .

How do I get the error handled within RunWorkerCompleted ?

Below is part of the code.

private void bgwMain_DoWorkSerial(object sender, DoWorkEventArgs e)
{
    startDownloadSerial();
}

private void bgwMain_ProgressChangedSerial(object sender, ProgressChangedEventArgs e)
{
    switch (e.ProgressPercentage)
    {
        case 0:
            msgTextDownload("Iniciando processo de download via Serial.");
            break;
        case 1:
            tmrDownloadDot = new Timer();
            tmrDownloadDot.Interval = 1000;
            tmrDownloadDot.Tick += TmrDownloadDot_Tick;
            msgTextDownload("Download iniciado.");
            msgTextDownload("");
            tmrDownloadDot.Start();
            break;
        case 2:
            tmrDownloadDot.Stop();
            msgTextDownload("Download finalizado.");
            break;

        default:
            throw new NotImplementedException();
    }
}



private void bgwMain_RunWorkerCompletedSerial(object sender, RunWorkerCompletedEventArgs e)
{
    if (e.Cancelled)
    {
        msgTextDownload("Operação cancelada pelo usuário.");
    }
    else if (e.Error != null)
    {
        msgTextDownload("Ocorreu um erro durante a aplicação: ");
        msgTextDownload("Source:" + e.Error.Source + "| Message: " + e.Error.Message);
    }
    else
    {
        msgTextDownload("Processo de download via serial finalizado.");
        if (e.Result != null)
        {
            try
            {
                msgTextDownload(e.Result.ToString());
            }
            catch (Exception ex)
            {

            }
        }
    }
}

In the code, the msgTextDownload method only prints the text in TextBox corresponding to BackgroundWorker .

    
asked by anonymous 28.11.2016 / 17:27

1 answer

3

Your code is right, the issue is that Debug mode is first thrown at new throw to just check and do other implementations / fixes.

If you use Release mode, only the message created in bgwMain_RunWorkerCompletedSerial will be displayed. Also in this mode, when executing your program (by VS) you will be asked about how you still want to proceed when debugging the code. Remember that in Release mode your code can be optimized, disrupting a detailed debug.

If you want to learn more, read the answered a question I've already asked here.

    
29.11.2016 / 13:05