1

I'm developing a small add-in for ESRI ArcGIS Explorer 1200. The extension itself is quite simple, it just uses a FileSystemWatcher to wait for an incoming file, then processes the file.

My main problem is: When the FileSystemWatcher event fires, it uses a different thread than the GUI-thread. So I can't access GUI-related objects. Now I would need some way to invoke a piece of code in the user thread, but I don't know how to do this in ArcGIS world.

My extension so far looks like this:

public class MyExtension : ESRI.ArcGISExplorer.Application.Extension
{
  FileSystemWatcher _fsw;

  public override void OnStartup()
  {
    _fsw = new FileSystemWatcher(@"c:\Temp\Import", "*.xml");
    _fsw.IncludeSubdirectories = false;
    _fsw.Created += FileCreated;
    _fsw.EnableRaisingEvents = true;
  }

  void FileCreated(object sender, FileSystemEventArgs e)
  {
    GraphicCollection graphic = ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay.Graphics; // <-- Threading Exception happens here
    MessageBox.Show(Convert.ToString(graphic.Count));
  }

  public override void OnShutdown()
  {
    _fsw.EnableRaisingEvents = false;
  }

}

Any ideas how to work around this?

4

1 回答 1

0

SynchronizationContext.Current保存来自 UI 线程的引用。
然后,您可以使用SynchronizationContext来自任何其他线程的此实例来调用回 UI 线程。

免责声明:我对 ArcGIS Explorer 一无所知;如果它不是 WinForms 或 WPF UI,这可能不起作用。

于 2010-07-22T15:51:42.547 回答