title: Where should TreeView's Invoke method between two WinForms be handled?
body: <p>I have two WinForms (<code>Setting</code> and <code>frmMain</code>). I have a TreeView in Setting's form and I want to call its <code>FillTree</code> method in the second form <code>frmMain</code>.</p>
  
  <p>I'm using the <code>TreeView.Invoke</code> for the threading purposes.</p>
  
  <p>Here is my code for TreeView filling data in Setting's form :</p>
  
  <pre><code>TreeNode parentNode;
  public void FillTree(DataTable dtGroups, DataTable dtGroupsChilds)
  {
      treeViewGroups.Nodes.Clear();
      if (dtGroups == null) return;
      foreach (DataRow rowGroup in dtGroups.Rows)
      {
          parentNode = new TreeNode
          {
              Text = rowGroup["Groupname"].ToString(),
              Tag = rowGroup["Groupid"]
          };
          treeViewGroups.Invoke(new Add(AddParent), new object[] { parentNode });
  
          if (dtGroupsChilds == null) continue;
          foreach (DataRow rowUser in dtGroupsChilds.Rows)
          {
              if (rowGroup["Groupid"] == rowUser["Groupid"])
              {
                  TreeNode childNode = new TreeNode
                  {
                      Text = rowUser["Username"].ToString(),
                      Tag = rowUser["Phone"]
                  };
                  treeViewGroups.Invoke(new Add(AddParent), new object[] { childNode });
                  System.Threading.Thread.Sleep(1000);
              }
          }
      }
      treeViewGroups.Update();
  }
  
  public delegate void Add(TreeNode tn);
  
  public void AddParent(TreeNode tn)
  {
      treeViewGroups.Nodes.Add(tn);
  }
  
  public void AddChild(TreeNode tn)
  {
      parentNode.Nodes.Add(tn);
  }
  </code></pre>
  
  <p><code>FillTree</code> method from above code, I wants call it in my second form frmMain which I tried like so:</p>
  
  <pre><code>Settings settingsWindow;
  public frmMain()
  {
      InitializeComponent();
  
      settingsWindow = new Settings(this);
  }
  
  private void SomeMethod()
  {
      //Two DataTables (dt1 and dt2) are passed from frmMain form
  
      settingWindow.FillTree(dt1, dt2);
  }
  </code></pre>
  
  <p>When I call <code>FillTree</code> method it show me error like this:</p>
  
  <blockquote>
    <p>Invoke or BeginInvoke cannot be called on a control until the window handle has been created.</p>
  </blockquote>
  
  <p>I real wants to know, where should be handle TreeView's Invoke method in my second winform <code>frmMain</code>?</p>
  
  <hr>
  
  <p>I'm following these links (but no avail):</p>
  
  <p>1) <a href="https://www.codeproject.com/Articles/27719/Populating-TreeView-on-a-Background-Thread" rel="noreferrer">Populating TreeView on a Background Thread</a></p>
  
  <p>2) <a href="https://stackoverflow.com/questions/36137127/how-to-add-object-in-treeview-from-another-thread">How to add object in treeview from another thread</a></p>
  
  <p>3) <a href="https://stackoverflow.com/questions/27276842/how-can-i-invoke-a-method-called-from-backgroundworker-dowork-event">How can i invoke a method called from backgroundworker dowork event?</a></p>
  
  <hr>
  
  <h1>Edited for Visualizing Problem</h1>
  
  <p>I have tried it with TreeView its not working then I Tried it with ListBox but the problem is still same.</p>
  
  <p><strong>Problem:</strong> I've a method (which populate my ListBox) in WinForm <code>settingsWindow</code> and I want to call that method in my second WinForm <code>frmMain</code>.</p>
  
  <p><code>Settings</code> form screenshot:</p>
  
  <p><a href="https://i.stack.imgur.com/NQvGg.png" rel="noreferrer"><img src="https://i.stack.imgur.com/NQvGg.png" alt="enter image description here"></a></p>
  
  <p><code>frmMain</code> form screenshot:</p>
  
  <p><a href="https://i.stack.imgur.com/g6T3d.png" rel="noreferrer"><img src="https://i.stack.imgur.com/g6T3d.png" alt="enter image description here"></a></p>
  
  <p>Problem GIF :</p>
  
  <p><a href="https://i.stack.imgur.com/0tnNY.gif" rel="noreferrer"><img src="https://i.stack.imgur.com/0tnNY.gif" alt="enter image description here"></a></p>
  
  <p>Settings form Code for ListBox Populating :</p>
  
  <pre><code>public void PopulateGroupListData(DataTable dt)
  {
      listGroups.DataSource = null;
  
      listGroups.DisplayMember = "GroupName";
      listGroups.ValueMember = "Groupid";
      listGroups.DataSource = dt;
  
      if (listGroups.Items.Count &gt; 0)
          listGroups.SelectedItem = listGroups.Items[0];
  }
  </code></pre>
  
  <p>Calling <code>PopulateGroupListData</code> in second form <code>frmMain</code> 's method:</p>
  
  <pre><code>void onCompleteReadFromServerStream(IAsyncResult iar)
  {
      /... some code
  
      String[] arr1 = ServerMessage[1].Split('&amp;');
      string Groupid1 = arr1[0];
      string GroupName1 = arr1[1];
      GroupsList.Rows.Add(Groupid1, GroupName1);
      settingsWindow.PopulateGroupListData(GroupsList);
  
      /... some code
  }
  </code></pre>