开发者

ArcGIS Explorer: Invoke main thread from secondary thread

开发者 https://www.devze.com 2023-01-06 23:37 出处:网络
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.

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
{
  FileSy开发者_如何学运维stemWatcher _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?


Save a reference to SynchronizationContext.Current from the UI thread.
You can then use this SynchronizationContext instance from any other thread to invoke back to the UI thread.

Disclaimer: I know nothing about ArcGIS Explorer; if it isn't a WinForms or WPF UI, this might not work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号