1

I have 2 winforms, one (call it Form1) contains a background worker, and that form opens a second form (Form2). Form1 starts a background worker, and while its working opens Form2 so the user can do something else. When the user is done on Form2 and clicks submit, I want to check if Form1's background worker is done and if it isn't pop a message asking the user to wait a minute, else allow the Form2 submit.

How can I check the status of Form1's background worker from Form2? I looked at this post Check to see if a thread in another form is still running , but my situation is different and I'm not sure how to adapt.

Community
  • 1
  • 1
MAW74656
  • 3,449
  • 21
  • 71
  • 118

2 Answers2

0

Check the BackgroundWorker.IsBusy property.

Edit

There are various options for making this information available to the second form.

  • You could pass the BackgroundWorker to Form2 on creation. Form2 can then check the IsBusy property.

  • You could create a public property IsRunningBackgroundWork on Form1, and give Form2 a reference to Form1. The getter for this property would check BackgroundWorker.IsBusy.

  • You could create a public property CanSubmit on Form2, and set it from Form1 when the BackgroundWorker completes.

    bgw.RunWorkerCompleted += (s, e) => form2.CanSubmit = true;

Alternatively (and I think this is best) you could put the submission code in Form1, and just use Form2 as a dialog. Then Form2 doesn't need to know anything about Form1. Form1 opens Form2 modally, and when Form2 closes, Form1 checks if the worker is finished, and acts accordingly.

Igby Largeman
  • 16,495
  • 3
  • 60
  • 86
0

The situation is almost similiar to that one provided in your question. You can do something like this:

public class Form1 
{
   public bool IsBackgroundWorkerStillRunning {get {....};}
}


public class Form2 
{
    Form1 form1 = null;
    public Form2(Form1 frm1)
    {
      form1 = frm1;
    }

     private void OnSubmit(...)
     {
         if(form1.IsBackgroundWorkerStillRunning )
              //wait 
         else
              //proceed
     }
}

one the Form1 have a property which checks for background worker state

Tigran
  • 61,654
  • 8
  • 86
  • 123