title: C# compile error: "Invoke or BeginInvoke cannot be called on a control until the window handle has been created."
body: <p>I just posted a question about how to get a delegate to update a textbox on another form. Just when I thought I had the answer using Invoke...this happens. Here is my code:</p>
  
  <p><strong>Main Form Code:</strong></p>
  
  <pre><code>using System;
  using System.Drawing;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;
  using System.IO;
  using System.Data.OleDb;
  using System.Collections.Specialized;
  using System.Text;
  using System.Threading;
  
  delegate void logAdd(string message);
  
  namespace LCR_ShepherdStaffupdater_1._0
  {
      public partial class Main : Form
      {
          public Main()
          {
              InitializeComponent();
          }
  
          public void add(string message)
          {
              this.Log.Items.Add(message);
          }
          public void logAdd(string message)
          {   /////////////////////////// COMPILER ERROR BELOW ///////////
              this.Invoke(new logAdd(add), new object[] { message }); // Compile error occurs here     
          }////////////////////////////// COMPILER ERROR ABOVE ///////////
  
          private void exitProgramToolStripMenuItem_Click(object sender, EventArgs e) 
          {
              Application.Exit(); 
          }
          private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
          {
              Form aboutBox = new AboutBox1(); 
              aboutBox.ShowDialog(); 
          }
  
          private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
          {
          }
  
          private void settingsToolStripMenuItem1_Click(object sender, EventArgs e)
          {
              settingsForm.settings.ShowDialog();
          }
  
          private void synchronize_Click(object sender, EventArgs e)
          {
              string message = "Here my message is"; // changed this
              ErrorLogging.updateLog(message);  // changed this
          }
  
      }
  
      public class settingsForm 
      {
          public static Form settings = new Settings();
      }
  
  }
  </code></pre>
  
  <p><strong>Logging Class Code:</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  namespace LCR_ShepherdStaffupdater_1._0
  {
      public class Logging
      {
          static Main mainClass = new Main();
          static logAdd logAddDelegate;
  
          public static void updateLog(string message)
          {
              logAddDelegate = mainClass.logAdd;
              logAddDelegate(message);
          }
      }
  }
  </code></pre>
  
  <ul>
  <li><p><strong>Compile Error:</strong> </p>
  
  <p><em>InvalidOperationException was
  unhandled - Invoke or BeginInvoke
  cannot be called on a control until
  the window handle has been created.</em></p></li>
  </ul>
  
  <p>I already tried to create a handle on the Log item...but that didn't work. The problem is I have NO CLUE what I am doing and I have searched Google <strong>extensively</strong> only to find vague answers.</p>
  
  <p>Please tell me how to create the handle before I invoke this delegate. While you are at it, give me some ways I can make this code more simple. For example, I dont want two Add functions... I had to do that because there was no way for me to find an item to invoke from the Logging class. Is there a better way to accomplish what I need to do?</p>
  
  <p>Thank you!!!</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>My project is fairly large, but these are the only items causing this specific problem.</p>
  
  <p><strong>Log</strong> is my RichTextBox1 (Log.Items.Add(message)) I renamed it to Log so it is easier to retype.</p>
  
  <p>I am calling updateLog(message) from a different form though...let me update that in here (although it makes no difference where I call updateLog(message) from it still gives me this error)</p>
  
  <p>You guys are going to have to make things more simpler for me...and provide examples. I don't understand HALF of everything you guys are saying here...I have no clue on how to work with Invoking of methods and Handles. I've researched the crap out of it too...</p>
  
  <p><strong>SECOND EDIT:</strong></p>
  
  <p>I believe I have located the problem, but do not know how to fix it.</p>
  
  <p>In my logging class I use this code to create mainClass:</p>
  
  <p><strong>static Main mainClass = new Main();</strong></p>
  
  <p>I am creating a entirely new blueprint replica to Main(), including <strong>Log</strong> (the richtextbox I am trying to update)</p>
  
  <p>When I call updateLog(message) I believe I am trying to update the Log (richtextbox) on the second entity of Main() otherwise known as mainClass. Of course, doing so will throw me this exception because I haven't even seen that replica of the current Main I am using.</p>
  
  <p>This is what I am shooting for, thanks to one of the people that gave an answer:</p>
  
  <pre><code>Main mainClass = Application.OpenForms.OfType&lt;Main&gt;().First();
  logAddDelegate = mainClass.logAdd; 
  logAddDelegate(message);
  </code></pre>
  
  <p>I need to create mainClass not with the new() operator because I dont want to create a new blueprint of the form I want to be able to edit the current form.</p>
  
  <p>The above code doesn't work though, I can't even find Application. Is that even C# syntax?</p>
  
  <p>If I can get the above code to work, I think I can resolve my issue and finally lay this problem to rest after a couple of HOURS of seeking for answers.</p>
  
  <p><strong>FINAL EDIT:</strong></p>
  
  <p>I figured it out thanks to one of the users below. Here is my updated code:</p>
  
  <p><strong>Main Form Code:</strong></p>
  
  <pre><code>using System;
  using System.Drawing;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;
  using System.IO;
  using System.Data.OleDb;
  using System.Collections.Specialized;
  using System.Text;
  using System.Threading;
  
  delegate void logAdd(string message);
  
  namespace LCR_ShepherdStaffupdater_1._0
  {
      public partial class Main : Form
      {
          private static Main mainFormForLogging;
          public static Main MainFormForLogging
          {
              get
              {
                  return mainFormForLogging;
              }
          }
  
          public Main()
          {
              InitializeComponent();
              if (mainFormForLogging == null)
              {
                  mainFormForLogging = this;
              }
          }
  
          public void add(string message)
          {
              this.Log.Items.Add(message);
          }
          public void logAdd(string message)
          {
              this.Log.BeginInvoke(new logAdd(add), new object[] { message });
          }
  
          private void exitProgramToolStripMenuItem_Click(object sender, EventArgs e) 
          {
              Application.Exit(); 
          }
          private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
          {
              Form aboutBox = new AboutBox1(); 
              aboutBox.ShowDialog(); 
          }
  
          private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
          {
          }
  
          private void settingsToolStripMenuItem1_Click(object sender, EventArgs e)
          {
              settingsForm.settings.ShowDialog();
          }
  
          private void synchronize_Click(object sender, EventArgs e)
          {
              add("test");
              Logging.updateLog("testthisone");
              //DatabaseHandling.createDataSet();
          }
  
      }
  
      public class settingsForm 
      {
          public static Form settings = new Settings();
      }
  
  }
  </code></pre>
  
  <p><strong>Logging Class Code:</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  namespace LCR_ShepherdStaffupdater_1._0
  {
      public class Logging
      {
  
          static Main mainClass = Main.MainFormForLogging;
          static logAdd logAddDelegate;
  
          public static void updateLog(string message)
          {
              logAddDelegate = mainClass.logAdd;
              logAddDelegate(message);
          }
      }
  }
  </code></pre>