2010-07-12 91 views
1

我正在爲ESRI ArcGIS Explorer 1200開發一個小插件。擴展本身非常簡單,它只是使用FileSystemWatcher等待傳入文件,然後處理文件。ArcGIS Explorer:從輔助線程調用主線程

我的主要問題是:當FileSystemWatcher事件觸發時,它使用與GUI線程不同的線程。所以我無法訪問與GUI相關的對象。 現在我需要一些方法來調用用戶線程中的一段代碼,但我不知道如何在ArcGIS世界中執行此操作。

我的擴展到目前爲止是這樣的:

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; 
    } 

} 

任何想法如何解決此問題?

回答

0

從UI線程保存對SynchronizationContext.Current的引用。
然後,您可以使用此SynchronizationContext實例從任何其他線程調用回UI線程。

聲明:我對ArcGIS Explorer一無所知;如果它不是WinForms或WPF UI,則可能不起作用。