ここでは、ArcMapで作業中にSTAスレッドを使用する必要があります。私は通常のBackgroudnWorker
を使用していましたが、私のコードは非常に遅いです。私はそれを変更しようとしているので、作業者が内部にSTAスレッドを作成し、それを「重い」もので実行するようにしています。ArcMap and BackgroundWorkerThread
私の問題は、2番目のスレッドが正常に動作した後、すべてのcomオブジェクトが解放されることです。私は何らかのマーシャリングをしているかどうか確認しました.RelaseComObjectかShutdownコールですが、そうは思いません。私はちょうどそれらのCOMオブジェクトを取得したスレッドが実行されているため、オブジェクトが自動的に解放されている可能性がありますか?ここ
は私のサンプルコードです:ライン#22で
private void bckgrndWrkrController_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
if (worker != null)
{
controller.BackgroundWorker = worker;
Thread thread = new Thread(STAProcessSelection);
thread.SetApartmentState(ApartmentState.STA);
thread.Start(e.Argument);
thread.Join();
e.Result = processingResult;
e.Cancel = worker.CancellationPending;
}
}
private void STAProcessSelection(object argument)
{
ISelection selection = argument as ISelection;
if (selection != null)
{
processingResult = controller.ProcessSelection(selection);
}
}
private void bckgrndWrkrController_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (e.Result is bool)
{
// Making sure the thread was not cancelled after we got the result
processingResult = (bool)e.Result && !worker.CancellationPending;
if (processingResult)
{
// Set the datasource of the grid
bindingSource.DataSource = controller.List;
}
}
// and inform the command we are done
OnDoneProcessing(EventArgs.Empty);
}
、ProcessSelection呼び出しの後、controller.Listは[0]有効なCOMオブジェクトが含まれています。 11行目で、thread.Join呼び出しの後で、controller.List [0]要素に、すでに解放されたcomオブジェクトが含まれています。 私はここで何が間違っていますか?