title: access backgroundworker from a public method
body: <p>So i have <code>form1</code> which has backgroundworker (dragged and dropped via design view).
  I can get it to work in the places i need, however I need to call it from a public method.</p>
  
  <p>In this public method </p>
  
  <pre><code>Utility.initpacks(object sender, EventArgs e,string formname)
  </code></pre>
  
  <p>SO my <code>DoWork</code> is in <code>Form1</code>.</p>
  
  <p>I the public utility within the form do do a bunch of things, then THAT function needs to use the background worker inside Form1 again!</p>
  
  <p>I could just copy the public method and put in the place of the method reference and all is well.. but that defeats the purpose of a public method doesn't it!?</p>
  
  <p>Any ideas would be great thanks :)</p>
  
  <p>EDIT: </p>
  
  <p>SO my current setup (without a bunch of stuff not important):</p>
  
  <pre><code>public partial class frmimportinstitutions : Form
      {
      private void btnNext_Click(object sender, EventArgs e)
           {
           Utility.initpacks(sender, e, this.FindForm().Name);
           }
  
  
  
  
      private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
          {
              //Do stuff
  
          }
  }
  
  public static class Utility
      {
  public static void initpacks(object sender, EventArgs e,string formname)
          {
  //I WANT TO USE THE BACKGROUND WORKER HERE
  //Do a public method
  //I want to stop the background worker here
  }
  }
  </code></pre>
  
  <p>Update (basd on comments):</p>
  
  <p>Michael <em>comments</em> just mentioned to put the background worker starting in a public method:</p>
  
  <pre><code>public void startplash(string starttext)
          {
              if (!backgroundWorker1.IsBusy)
              {
                  splashtext = starttext;
                  backgroundWorker1.RunWorkerAsync();
              }
  
          }
  </code></pre>
  
  <p>Now i want to call this method from the other method.  In order to do this, the other method (init packs) needs to know where this method is doesnt it.
  EG.</p>
  
  <pre><code>form1.startsplash("hello world")
  </code></pre>
  
  <p>So now i just need to send Form1 info to init packs...</p>
  
  <p>Would this be ok:</p>
  
  <pre><code>Initpacks(Form Owner)
  {
  Owner.startsplash("hello world")
  }
  </code></pre>
  
  <p>Another update!
  Thanks for Michael we so far have this:</p>
  
  <pre><code>public static class Utility
  {
      public static void RunWorkerOfForm1()
      {
          var target = (Form1)Application.OpenForms.OfType&lt;Form1&gt;().FirstOrDefault();
          target?.RunWorker();
      }
  }
  </code></pre>
  
  <p>Now I need to get this to work with different forms.. I havent tried the below but this is what i am going to try next.. correct me if i am wrong:</p>
  
  <pre><code>public static class Utility
  {
      public static void RunWorkerOfForm1(Form owner)
      {
          var target = (owner)Application.OpenForms.OfType&lt;owner&gt;().FirstOrDefault();
          target?.RunWorker();
      }
  }
  </code></pre>
  
  <p>Final Answer (as per the ticked answer) - but using my code:</p>
  
  <pre><code>public partial class frmholidaypacks : Form, IWorker
      {
  private void btnextrapacks_Click(object sender, EventArgs e)
          {
           Utility.futurepacks&lt;frmholidaypacks&gt;(sender, e, pxid);
           }
       }
  
  public interface IWorker
      {
          void startplash(string starttext);
  
      }
  
  public void startplash(string starttext)
          {
              if (!backgroundWorker1.IsBusy)
              {
                  splashtext = starttext;
                  backgroundWorker1.RunWorkerAsync();
              }
  
          }
  
  private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
          {
              //Doing work.  Using Splashtext string.
  
          }
  
  public static void futurepacks&lt;T&gt;(object sender, EventArgs e, int pxid) where T : IWorker
          {
   var target = (T)Application.OpenForms.OfType&lt;T&gt;().FirstOrDefault();
              target?.startplash("Creating future packs");
          }
  </code></pre>
  
  <p>100% Credit goes to Michael for working on this with me!</p>