C# ArcMap和BackgroundWorkerThread

C# ArcMap和BackgroundWorkerThread,c#,multithreading,com,arcgis,C#,Multithreading,Com,Arcgis,我在这里读到,在ArcMap中工作时,我应该坚持使用STA线程。我使用的是一个普通的后台工作人员,我的代码运行得非常慢。我试图改变它,这样工人就可以在里面创建一个STA线程,让它在重的东西上运行 我现在的问题是,在第二个线程完成工作后,所有com对象都被释放。我检查了是否有某种marshal.RelaseComObject或Shutdown调用,但我认为情况并非如此。难道我只是因为检索那些com对象的线程已经运行完毕,这些对象就被自动释放了吗 以下是我的代码示例: private voi

我在这里读到,在ArcMap中工作时,我应该坚持使用STA线程。我使用的是一个普通的后台工作人员,我的代码运行得非常慢。我试图改变它,这样工人就可以在里面创建一个STA线程,让它在重的东西上运行

我现在的问题是,在第二个线程完成工作后,所有com对象都被释放。我检查了是否有某种marshal.RelaseComObject或Shutdown调用,但我认为情况并非如此。难道我只是因为检索那些com对象的线程已经运行完毕,这些对象就被自动释放了吗

以下是我的代码示例:

    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);
    }
在第22行,在ProcessSelection调用之后,controller.List[0]包含一个有效的com对象。在第11行,在thread.Join调用之后,controller.List[0]元素已经包含一个已发布的com对象。
我在这里做错了什么?

我读到单线程公寓只允许有一个线程在里面,我不认为这是显而易见的。。。。所以,尽管我的主线程是STA,我创建了另一个STA线程,但它们位于不同的隔间。 当我的第二个线程完成了他的工作,上帝处理了,在公寓里调用COM对象的代码无法执行,没有线程来处理调用。也许再也没有COM对象了

无论如何,我仍然不知道如何在ArcMAP中有效地使用BackgroundWorker。但我认为这解释了这次尝试失败的原因