Click here to Skip to main content
15,941,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have an application in C# that uses a backgroundWorker to make some calculations. The current operation performed by the backgroundWorker is displayed to a Form that is created by the main thread, in a ListBox. When the backgroundWorker has finished it's work the form remains open until the user closes it. The problem is that the first time that the backgroundWorker starts it works fine, but the second time is not doing it's job(not putting stuff in the ListBox and not performing the operation). But, if I put a breakpoint in the backgroundWorker_DoWork() and go step by step it works fine. Can someone give me some help with this?

This is the part of the method that calls the backgroundWorker
 if (workYourBack.IsBusy == true)
 {
     try
     {
         workYourBack.CancelAsync();

     }
     catch(System.InvalidOperationException ex)
     {
         MessageBox.Show("No se puede lanzar.");
         return;
     }
 }


 try
 {
     _sW.Close();
 }
 catch (System.InvalidOperationException ex)
 {
     MessageBox.Show("Invalid operation.");
     return;
 }
 catch (System.NullReferenceException ex1)
 {

 }


 _sW = new StatusWindow();
 workYourBack.RunWorkerAsync(mlParam);
_sW.ShowDialog();


backgroundWorker.DoWork()
MIDL
           _sW.Invoke((MethodInvoker)delegate()
           {
               if (_sW.Visible == false)
               _sW.ShowDialog();
           });
          _sW.Invoke((MethodInvoker)delegate()
          {
              _sW.InsertText("<<<OBTENER INFORMACIÓN DEL CENTRO>>>");
              _sW.InsertText("Procesando capas.......");
          }
    
          //work to be done and various calls to _sW.Invoke

         

);

Thanks.
Posted

1 solution

I think your problem maybe to do with calling ShowDialog from outside and potentially inside the worker thread. I suggest you create you work in Form_Load then call ShowDialog from outside.

C#
if (_sW.Visible == false)
              _sW.ShowDialog();

if the form is shown but not visible make it visible like this
C#
if (_sW.Visible == false)
              _sW.Visible = true;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900