title: TreeView does not update when modified. Windows Forms
body: <p>I have a Windows Forms C# application which deals with TreeView and uses a separate thread to update it. 
  Update launched by button click:</p>
  
  <pre><code>    private void button3_Click(object sender, EventArgs e)
      {
          vc.ChangeTree(treeView1);
      }
  </code></pre>
  
  <p>ChangeTree() starts UpdateTree() in a separate thread to update the TreeView asynchronously. 
  Here I just need to use Thread for this:</p>
  
  <pre><code>    public void ChangeTree(TreeView tree)
      {
          Thread thread4 = new Thread(() =&gt; { UpdateTreeView(tree); });
          thread4.Name = "Thread 4";
          thread4.IsBackground = true;
          thread4.Start();
      }
  </code></pre>
  
  <p>UpdateTree() simply invokes the TreeView and adds nodes from another tree to it. Note that there aren't time consuming operations. The GetTree() just returns another TreeView: </p>
  
  <pre><code>    private void UpdateTreeView(TreeView tree)
      {
          if (tree.InvokeRequired)
          {
              tree.Invoke((MethodInvoker)delegate
              {
                  UpdateTreeView(tree);
              });
          }
          else
          {
              tree.Nodes.Clear();
              foreach (var node in GetTree().Nodes)
              {
                  tree.Nodes.Add(node as TreeNode);
              }
              //Application.DoEvents(); // doesn't help
          }
      }
  </code></pre>
  
  <p>The problem is that it actually adds nodes to the tree, but form does not show it. I checked the treeView1.Nodes after i clicked the button. It contains the nodes I need. </p>
  
  <p>The Application.DoEvents() does not help, even though it may, according to my search. Neither does treeView1.Refresh().</p>
  
  <p>If i click the button twice, treeView1 updates as I planned. But I don't know why and, obviously, I don't need such behavior.</p>
  
  <p>So here is the question. How to make it update with one click?</p>
  
  <hr>
  
  <p>It turns out that TreeNode can't be used by more than one TreeView. </p>
  
  <p><strong>The solution</strong> I found is to use treeNode.Clone():</p>
  
  <pre><code>tree.Nodes.Add((node as TreeNode).Clone() as TreeNode);
  </code></pre>
  
  <p><strong>But</strong> I still don't understand why the initial code worked, even though it took two times for it.</p>