CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2010
    Posts
    1

    problem with 2Forms+progressbar+background worker

    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.

  2. #2
    Join Date
    Sep 2010
    Posts
    23

    Re: problem with 2Forms+progressbar+background worker

    So if you can access objects on form1 from form 2, all is okay ?

    The backgroundworker modifier must be PUBLIC. , i think its private by default.

    When you create form2 , you must pass the main form as an argument, to the constructor of form 2.

    That goes like this:

    Code:
    newform Form2 = new Form2(this);
    notice the THIS, which refers to the current form your working on.
    Now, you need to take the code of form2, and look for the constructor, and add a parameter:

    Code:
    public Form1(Form1 parent)
    Now, you passed, a reference to the main form, to the constructor of form2, which you can access in Form2 using: parent
    EX: parent.backgroundworker

    To access the parent (the main form) from anywhere in form2, make it globally available
    by creating another object of Form1 type, in a global scope, and assigning the passed object, to it.

    Code:
        public partial class Form1 : Form
        {
    
           private Form1 parent;
    
            public Form1(Form1 parent)
            {
                InitializeComponent();
                this.parent = parent;
            }
        }
    }


    I hope, i explained this correctly.

  3. #3
    Join Date
    Sep 2010
    Posts
    23

    Re: problem with 2Forms+progressbar+background worker

    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).
    Isnt that because your progress bar is running on form2 ?

    If so, put the progress bar on another thread/background worker, and that problem should be solved.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: problem with 2Forms+progressbar+background worker

    There is no need to make things public like that mavy. Just use events to communicate between forms.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured