title: c# opennetCF background worker - e.result gives a ObjectDisposedException
body: <p>I'm new working with background worker in C#.
  Here is a class, and under it, you will find the instansiation of it, and under there i will define my problem for you:</p>
  
  <p>I have the class Drawing:</p>
  
  <pre><code>class Drawing
  {
      BackgroundWorker bgWorker;
      ProgressBar progressBar;
      Panel panelHolder;
  
      public Drawing(ref ProgressBar pgbar, ref Panel panelBig)  // Progressbar and panelBig as reference
      {
          this.panelHolder = panelBig;
          this.progressBar = pgbar;
          bgWorker = new BackgroundWorker();
          bgWorker.WorkerReportsProgress = true;
          bgWorker.WorkerSupportsCancellation = true;
  
          bgWorker.DoWork += new OpenNETCF.ComponentModel.DoWorkEventHandler(this.bgWorker_DoWork);
          bgWorker.RunWorkerCompleted += new OpenNETCF.ComponentModel.RunWorkerCompletedEventHandler(this.bgWorker_RunWorkerCompleted);
          bgWorker.ProgressChanged += new OpenNETCF.ComponentModel.ProgressChangedEventHandler(this.bgWorker_ProgressChanged);
      }
  
      public void createDrawing()
      {
          bgWorker.RunWorkerAsync();
      }
  
      private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
      {
         Panel panelContainer = new Panel();
  
            // Adding panels to the panelContainer
            for(i=0; i&lt;100; i++)
            {
              Panel panelSubpanel = new Panel();
              // Setting size, color, name etc....
  
               panelContainer.Controls.Add(panelSubpanel);  // Adding the subpanel to the panelContainer
  
               //Report the progress
               bgWorker.ReportProgress(0, i); // Reporting number of panels loaded
            }
  
            e.Result = panelContainer;   // Send the result(a panel with lots of subpanels) as an argument 
      }
  
      private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
      {
            this.progressBar.Value = (int)e.UserState; 
            this.progressBar.Update();
      }
  
      private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
      {
          if (e.Error == null)
          {
              this.panelHolder          = (Panel)e.Result; 
          }
          else
          {
              MessageBox.Show("An error occured, please try again");
          }
      }
  
  }
  </code></pre>
  
  <p>Instansiating an object of this class:</p>
  
  <pre><code>public partial class Draw: Form
  {
    public Draw()
    {
  
  
        ProgressBar progressBarLoading = new ProgressBar();
        // Set lots of properties on progressBarLoading 
  
        Panel panelBigPanelContainer = new Panel();          
  
        Drawing drawer = new Drawing(ref progressBarLoading, ref panelBigPanelContainer);
  
        drawer.createDrawing(); // this makes the object start a new thread, loading all the panels into a panel container, while also sending the progress to this progressbar.
    }
  
  }
  </code></pre>
  
  <p>Here is my problem:
  In the private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)</p>
  
  <p>i don't get the e.Result as it should be. 
  When i debug and look at the e.Result, the panel's properties have this exception message:</p>
  
  <pre><code>'((System.Windows.Forms.Control)(e.Result)).ClientSize' threw an exception of type    'System.ObjectDisposedException'
  </code></pre>
  
  <p>So the object gets disposed, but "why" is my question, and how can i fix this?</p>
  
  <p>I hope someone will answer me, this is making me crazy.
  Another question i have: Is it allowed to use "ref" with arguments? is it bad programming?</p>
  
  <p>Thanks in advance.</p>
  
  <p>I have also written how i understand the Background worker below here:</p>
  
  <hr>
  
  <p>This is what i think is the "rules" for background workers:</p>
  
  <pre><code>bgWorker.RunWorkerAsync();   =&gt; starts a new thread.
  bgWorker_DoWork cannot reach the main thread without delegates
  </code></pre>
  
  <p>-</p>
  
  <pre><code>private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
  {
         // The work happens here, this is a thread that is not reachable by 
            the main thread
  
         e.Result  =&gt; This is an argument which can be reached by
                      bgWorker_RunWorkerCompleted()
  
  
         bgWorker.ReportProgress(progressVar);  =&gt; Reports the progress to the
                                                   bgWorker_ProgressChanged()           
  
  }
  </code></pre>
  
  <p>-</p>
  
  <pre><code>    private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
      {
             // I get the progress here, and can do stuff to the main thread from here 
                (e.g update a control)
  
                this.ProgressBar.Value = e.ProgressPercentage;
       }
  </code></pre>
  
  <p>-</p>
  
  <pre><code>    private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
      {
          // This is where the thread is completed.
          // Here i can get e.Result from the bgWorker thread
          // From here i can reach controls in my main thread, and use e.Result in my main thread
  
  
          if (e.Error == null)
          {
              this.panelTileHolder  = (Panel)e.Result;
  
          }
          else
          {
              MessageBox.Show("There was an error");
          }
      }
  </code></pre>