How does Backgroundworker e.error work?

Asked

Viewed 72 times

1

I am creating an application that makes multiple connections simultaneously. For each connection I create one TextBox and a BackgroundWorker. When executing the DoWork of BackgroundWorker I use several ProgressPercentage that initiate various types of functionality, from starting a timer to placing messages on TextBox.

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

How do I get the error treated within the RunWorkerCompleted?

Below follows 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 method msgTextDownload only prints the text in TextBox corresponding to BackgroundWorker.

1 answer

3


Your code is right, the point is that in mode Debug is first launched (stop point) into the new throw to precisely check and make other implementations/ corrections.

If you use the mode Release, only the message created on bgwMain_RunWorkerCompletedSerial will be the exhibited. Still in this mode, when running your program (by VS) you will be asked how you still want to proceed when debugging the code. Remembering that in Release mode your code can be optimized, disturbing a detailed "debug".

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

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.