首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ArcMap和BackgroundWorkerThread

ArcMap和BackgroundWorkerThread
EN

Stack Overflow用户
提问于 2009-05-10 12:35:22
回答 1查看 327关注 0票数 0

我在这里读到,在ArcMap中工作时,我应该坚持使用STA线程。我使用的是一个普通的BackgroudnWorker,我的代码运行得非常慢。我正在尝试更改它,以便worker在内部创建一个STA线程,并让它在“繁重”的东西上运行。

我现在的问题是,在第二个线程完成工作后,我所有的com对象都会被释放。我检查了是否有某种marshal.RelaseComObject或关机调用,但我认为不是这样的。会不会是因为检索这些com对象的线程已经运行完毕,这些对象才会被自动释放?

下面是我的代码示例:

代码语言:javascript
复制
    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包含一个有效的com对象。在第11行,在thread.Join调用之后,controller.List元素已经包含一个已释放的com对象。我在这里做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-07-01 06:10:19

我读到单线程公寓(STA)只允许在它们内部有一个线程(我不认为这是显而易见的…)。因此,尽管我的主线程是STA,并且我创建了另一个STA线程,但它们位于不同的间隔中。

当我的第二个线程完成了他的工作,上帝处理掉了,在那个单元中调用COM对象的代码就不能执行了(没有线程可以封送调用。也许甚至不再有COM对象了?)

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/845256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档