Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It is correct that you should use STA threads when working with the COM objects in ArcGIS. Still, you can get the convenience of the BackgroundWorker, which always is an MTA thread from the system's thread pool.</p> <pre><code>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; } } </code></pre> <p>The STA thread can now use the BackgroundWorker's methods, such as reporting progress, checking for cancellation and reporting results.</p> <pre><code>protected virtual void StatusUpdateNotify(ProgressState progressState) { if (BackgroundWorker.CancellationPending) { throw new OperationCanceledException(); } BackgroundWorker.ReportProgress(progressState.Progress, progressState); } </code></pre> <p>In addition to only using STA threads when operating on ArcGIS objects, you should not share objects between two threds. From your code it seems like you access the GUI from the background worker: <code>lblStatus.Text = "Done...";</code>, which could be done in e.g. the delegate for RunWorkerComplete.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload