problem with 2forms+progressbar+background worker

Sep 19 2010 4:12 AM

Dear all,

I am having a very frustrating problem with a project in Visual Studio 2008 C#. I have tried everything, I have searched everywhere, but it seems that nothing works. I would appreciate if someone gave me some directions or suggestions, as to where to look for the solution.

What I want to do:

I have 2 forms. The MAIN form, runs a heavy image processing algorithm, using a background worker. The second form, FORM2 must have a progress bar, which will display the progress of the image processing executed in MAIN form. It must also have a CANCEL button, which will allow the canceling of the image processing algorithm.

 

What I have done so far:

1.       I have managed to solve the problem using only one form: there is no FORM2. The progress bar and the cancel button are on the MAIN form. Everything works fine. But this is not the thing I have to do! I really need the cancel button and the progress bar on a different form.

2.       I have managed to show the progress bar on FORM2 running smoothly, but I cannot interact with FORM2 (the mouse pointer becomes a hourglass when I hover it over FORM2, thus, I cannot press the CANCEL or the X button).

In order to do that, I wrote the following code:

 

In MAIN form:

 FORM2 PR = new FORM2();

PR.Show();                      

                       

for (i = 0; i < imsize_y; i++)

{

      //calculate the progress degree and store it in 'Process' variable

 

      PR.progressBar1.Value = Process;

      PR.Refresh(); //otherwise the PR form is not redrawn

 

 

      if (backgroundWorker1.CancellationPending   

      {

              PR.Close();//where the code stops in case cancel is pressed

              break;

      }

 

                          

      for (j = 0; j < imsize_x; j++)

      {

              //heavy image processing

 

      }

}

 

PR.Close();

 

 

 

 

3.       I have also tried to use PR.ShowDialog() instead of PR.Show(),in order have focus on FORM2. However, in this case, the image processing stops until and I have to press something in FORM2 in order to proceed with the image processing in MAIN form.

 

I know that this has to do something with threading, but I don't know what. When the MAIN form runs, I cannot interact with FORM2. I have found examples where I can communicate variables between forms, but, in these examples, there is no background worker involved.

 

I would appreciate any suggestions or directions, as to where to look for this solution.


Answers (1)