How to add object in treeview from another thread

Viewed 2296

I have a multithreading app, and I need to add object from another thread to the treeview. But I keep getting an exception

Action being performed on this control is being called from the wrong thread. Marshal to the correct thread using Control.Invoke or Control.BeginInvoke to perform this action.

Here is my code

ThreadPool.QueueUserWorkItem(new WaitCallback(GetFiles), entryPoint);

private void GetFiles(object entryPoint) 
{ 
      var localData = entryPoint as EntryPoint; 
      this.GetFiles(localData.DirectoryInfo, localData.TreeNode); 
      localData.ManualEvent.Set(); 
}

private void GetFiles(DirectoryInfo directory, TreeNode tree) 
{ 
    for (int i = 0; i < allFiles.GetLength(0); i++) 
    { 
        tree.Nodes.Add(allFiles[i].Name);
    }
}
2 Answers
Related