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
.