title: .net Custom Form Even Handlers getting called from different thread and crashing VS
body: <p>I am using Visual Studio 2012 to make a .dll that contains a class that extends the Windows Forms TreeView form.  My custom TreeView is called FolderTreeView.  Inside of it I add some private fields that I need, mainly being a List of Tuple that contains a DriveInfo and an associated FileSystemWatcher.  </p>
  
  <pre>
  
  foreach (var drive in DriveInfo.GetDrives())
              {
                  if (drive.IsReady == true)
                  {
                      FileSystemWatcher watcher = new FileSystemWatcher(drive.RootDirectory.FullName);
                      //_drives is List of Tuples
                      _drives.Add(new Tuple&ltDriveInfo, FileSystemWatcher&gt(drive, watcher));
                      watcher.NotifyFilter = NotifyFilters.DirectoryName;
                      watcher.IncludeSubdirectories = true;
                      watcher.Created += new FileSystemEventHandler(FileSystemWatcher_OnCreated);
                      watcher.Changed += new FileSystemEventHandler(FileSystemWatcher_OnChange);
                      watcher.Deleted += new FileSystemEventHandler(FileSystemWatcher_OnDelete);
                      watcher.Renamed += new RenamedEventHandler(FileSystemWatcher_OnRename);
                      watcher.EnableRaisingEvents = true;
  
                      Nodes.Add(drive.RootDirectory.Name);
  
                  }
              }
  </pre>
  
  <p>This code causes two problems, both I suspect with the even handlers.  The first problem is that the events for the FileSystemWatcher are getting called from a different thread, so it's throwing exceptions since other threads shouldn't be allowed to access Windows Forms.</p>
  
  <p>The second issue is that if the code where I set the Renamed event handler for the FileSystemWatcher is NOT commented out and I change a folder name in Windows Explorer, Visual Studios crashes, and I have no idea why.  I seems like it's very likely to be caused by the Renamed event handler.  </p>
  
  <p>I would like help trying to fix the thread problem first, because maybe that will fix the crashes unless there is another reason this may be happening.  Also, would it be better to handle all the Filesystem stuff and node building in a different class, and just get a node from said class and give it to a regular TreeView?  </p>
  
  <p>EDIT: I do believe it has to do with threads.  When it crashes, I can debug in another instance of Visual Studios, and I am getting null reference exceptions past where I have breakpoints.  This gives me reason to believe that the events are being triggered in another thread, so my breakpoints are not getting hit by the thread it thinks it's supposed to be on?</p>