线程和ArcGIS

时间:2009-03-18 13:42:16

标签: c# multithreading backgroundworker arcgis

我刚刚偶然发现了Backgroundworker对象,它似乎是我正在寻找的工具,可以在执行计算时使我的GUI响应。我正在为ArcGIS编写IO插件。

我正在ArcGIS外部进行一些数据处理,使用backgroundworker可以正常工作。但是当我将数据插入ArcGIS时,后台工作人员似乎将持续时间增加了9倍左右。将处理代码放在DoWork方法之外,可以将性能提高9倍。

我已经在网上读过这几个地方,但我没有多线程编程的经验,像STA和MTA这样的术语对我来说毫无意义。 link text 我也试图使用一个简单的线程实现,但结果相似。

有谁知道我能做些什么才能使用ArcGIS处理和维护响应式GUI?

编辑:我已经包含了我与后台工作者的交互示例。如果我将位于StartImporting方法中的代码放在cmdStart_Click方法中,它的执行速度会快得多。

private void StartImporting(object sender, DoWorkEventArgs e)
{
    DateTime BeginTime = DateTime.Now;
    // Create a new report object.
    SKLoggingObject loggingObject = new SKLoggingObject("log.txt");
    loggingObject.Start("Testing.");

    SKImport skImporter = new SKImport(loggingObject);
    try
    {
        // Read from a text box - no writing.
    skImporter.Open(txtInputFile.Text);
    }
    catch
    {
    }
    SKGeometryCollection convertedCollection = null;

    // Create a converter object.
    GEN_SK2ArcGIS converter = new GEN_SK2ArcGIS(loggingObject);

    // Convert the data.
    convertedCollection = converter.Convert(skImporter.GetGeometry());

    // Create a new exporter.
    ArcGISExport arcgisExporter = new ArcGISExport(loggingObject);

    // Open the file.            
    // Read from a text box - no writing.
    arcgisExporter.Open(txtOutputFile.Text);

    // Insert the geometry collection.
    try
    {
    arcgisExporter.Insert(convertedCollection);
    }
    catch
    {
    }
    TimeSpan totalTime = DateTime.Now - BeginTime;
    lblStatus.Text = "Done...";

}

private void ChangeProgress(object sender, ProgressChangedEventArgs e) 
{
    // If any message was passed, display it.
    if (e.UserState != null && !((string)e.UserState).Equals(""))
    {
    lblStatus.Text = (string)e.UserState;
    }
    // Update the progress bar.
    pgStatus.Value = e.ProgressPercentage;
}

private void ImportDone(object sender, RunWorkerCompletedEventArgs e)
{
    // If the process was cancelled, note this.
    if (e.Cancelled)
    {
    pgStatus.Value = 0;
    lblStatus.Text = "Operation was aborted by user...";
    }
    else
    {
    }

}

private void cmdStart_Click(object sender, EventArgs e)
{
    // Begin importing the sk file to the geometry collection.

    // Initialise worker.
    bgWorker = new BackgroundWorker();
    bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(ImportDone);
    bgWorker.ProgressChanged += new ProgressChangedEventHandler(ChangeProgress);
    bgWorker.DoWork += new DoWorkEventHandler(StartImporting);
    bgWorker.WorkerReportsProgress = true;
    bgWorker.WorkerSupportsCancellation = true;

    // Start worker.
    bgWorker.RunWorkerAsync();

}

private void cmdCancel_Click(object sender, EventArgs e)
{
    bgWorker.CancelAsync();
}

亲切的问候,卡斯帕

3 个答案:

答案 0 :(得分:3)

在ArcGIS中使用COM对象时,应该使用STA线程是正确的。尽管如此,您仍然可以获得BackgroundWorker的便利,后者始终是系统线程池中的MTA线程。

private static void OnBackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = (BackgroundWorker)sender;
    ToolToStart tool = e.Argument as ToolToStart;

    if (tool != null)
    {
        tool.BackgroundWorker = worker;

        // The background worker thread is an MTA thread, 
        // and should not operate on ArcObjects/COM types.
        // Instead we create an STA thread to run the tool in.
        // When the the tool finishes the infomation from the STA thread 
        // is transferred to the background worker's event arguments.
        Thread toolThread = new Thread(STAThreadStart);
        toolThread.SetApartmentState(ApartmentState.STA);
        toolThread.Start(tool);

        toolThread.Join();
        e.Cancel = m_ToolCanceled;
        e.Result = m_ToolResult;
    }
}

STA线程现在可以使用BackgroundWorker的方法,例如报告进度,检查取消和报告结果。

protected virtual void StatusUpdateNotify(ProgressState progressState)
{
    if (BackgroundWorker.CancellationPending)
    {
        throw new OperationCanceledException();
    }

    BackgroundWorker.ReportProgress(progressState.Progress, progressState);
}

除了在ArcGIS对象上操作时仅使用STA线程,您不应在两个thred之间共享对象。从您的代码中,您似乎可以从后台工作人员lblStatus.Text = "Done...";访问GUI,这可以在例如RunWorkerComplete的委托。

答案 1 :(得分:1)

通常,为了维护响应式GUI,您需要执行在不同线程中执行工作的代码。使用BeginInvoke方法使用.net非常容易:http://msdn.microsoft.com/en-us/library/aa334867(VS.71).aspx

简而言之,将所有非GUI代码包含在单独的类(或类)中,而不是直接调用每个方法,您创建一个委托并在其上调用BeginInvoke方法。然后该方法将关闭,并且不与GUI进一步交互。如果您希望更新GUI(例如进度条),那么您可以从类中引发事件并从GUI中捕获它们,但是您需要确保以线程安全的方式更新控件。如果希望GUI在方法完成时更新,则可以使用EndInvoke方法来处理

答案 2 :(得分:1)

我一直在努力寻找解决方案,以下是我最终要做的事情。代码是从各种文件中剪切和粘贴的,并提供给我一个想法。它演示了如何使用线程调用与ArcGIS通信的方法。该代码允许我更新主线程中的GUI,中止操作,并执行后期操作。我最终使用了我最初发布的链接中的第一个线程部分。

最初性能下降的原因可能是ArcGIS需要的单线程单元(STA)。 Backgroundworker似乎是MTA,因此不适合使用ArcGIS

好吧,我希望我没有忘记任何事情,并且可以自由地编辑我的解决方案。它既可以帮助我,也可能帮助其他人为ArcGIS开发东西。

public class Program
{
    private volatile bool AbortOperation;
    Func<bool> AbortOperationDelegate;
    FinishProcessDelegate finishDelegate;
    UpdateGUIDelegate updateGUIDelegate;

    private delegate void UpdateGUIDelegate(int progress, object message);
    private delegate void FinishProcessDelegate();

    private void cmdBegin_Click(...)
    {
        // Create finish delegate, for determining when the thread is done.
        finishDelegate = new FinishProcessDelegate(ProcessFinished);
        // A delegate for updating the GUI.
        updateGUIDelegate = new UpdateGUIDelegate(UpdateGUI);
        // Create a delegate function for abortion.
        AbortOperationDelegate = () => AbortOperation;

        Thread BackgroundThread = new Thread(new ThreadStart(StartProcess));            
        // Force single apartment state. Required by ArcGIS.
        BackgroundThread.SetApartmentState(ApartmentState.STA);
        BackgroundThread.Start();
    }

    private void StartProcess()
    {    
        // Update GUI.
        updateGUIDelegate(0, "Beginning process...");

        // Create object.
        Converter converter = new Converter(AbortOperationDelegate);
        // Parse the GUI update method to the converter, so it can update the GUI from within the converter. 
        converter.Progress += new ProcessEventHandler(UpdateGUI);
        // Begin converting.
        converter.Execute();

        // Tell the main thread, that the process has finished.
        FinishProcessDelegate finishDelegate = new FinishProcessDelegate(ProcessFinished);
        Invoke(finishDelegate);

        // Update GUI.
        updateGUIDelegate(100, "Process has finished.");
    }

    private void cmdAbort_Click(...)
    {
        AbortOperation = true;
    }

    private void ProcessFinished()
    {
        // Post processing.
    }

    private void UpdateGUI(int progress, object message)
    {
        // If the call has been placed at the local thread, call it on the main thread.
        if (this.pgStatus.InvokeRequired)
        {
            UpdateGUIDelegate guidelegate = new UpdateGUIDelegate(UpdateGUI);
            this.Invoke(guidelegate, new object[] { progress, message });
        }
        else
        {
            // The call was made on the main thread, update the GUI.
            pgStatus.Value = progress;
            lblStatus.Text = (string)message;   
        }
    }
}

public class Converter
{
    private Func<bool> AbortOperation { get; set;}

    public Converter(Func<bool> abortOperation)
    {
        AbortOperation = abortOperation;
    }

    public void Execute()
    {
        // Calculations using ArcGIS are done here.
        while(...) // Insert your own criteria here.
        {
            // Update GUI, and replace the '...' with the progress.
            OnProgressChange(new ProgressEventArgs(..., "Still working..."));

            // Check for abortion at anytime here...
            if(AbortOperation)
            {
                return;
            }
        }
    }

    public event ProgressEventHandler Progress;
    private virtual void OnProgressChange(ProgressEventArgs e)
    {
        var p = Progress;
        if (p != null)
        {
            // Invoke the delegate. 
        p(e.Progress, e.Message);
        }
    }    
}

public class ProgressEventArgs : EventArgs
{
    public int Progress { get; set; }
    public string Message { get; set; }
    public ProgressEventArgs(int _progress, string _message)
    {
        Progress = _progress;
        Message = _message;
    }
}

public delegate void ProgressEventHandler(int percentProgress, object userState);