ArcGIS Explorer: Invoke main thread from secondary thread
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.
精彩评论