What tis means is that every window and its controls is owned by a thread. Whenever you manipulate the window from the "wrong" thread there's a chance things might not work well, and sometimes an exception is thrown.
Control.Invoke and Control.BeginInvoke are methods that transfer execution to the owning thread.
What you need to do (and I know this is a bit cruddy) is something like this:
Code:
private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new System.IO.FileSystemEventHandler(fileSystemWatcher1_Changed), new object[] {e});
}
else
{
MyTree.Nodes.Add(e.ToString());
}
}
This will result in the methood calling itself on the correct thread if needed.
Using BeginInvoke(), rather than Invoke() makes the call asyncronous. Most of the time it doesn't matter, but when you're not interested in the result anyway it can sometimes create a more responsive app.
Rune
PS. Ignore the spurious space that the forum inserts in the middle of the BeginInvoke call...
If you hit a brick wall, you didn't jump high enough!
Bookmarks