WPF应用程序主线程上的COMException [英] COMException on Main Thread of WPF application

查看:236
本文介绍了WPF应用程序主线程上的COMException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF,Excel插件,C#, 我有多个异步调用,以便从主线程上的Web服务获取数据,然后在回叫中, 我将在Excel中绘制数据.我跟踪了回叫,它们也运行在主线程上. 但我仍然收到COMException 0x800AC472,用谷歌搜索,这似乎是一个多线程问题.

WPF, Excel AddIn, C#, I have multiple asychronous calls to get data from web service on main thread, then in call back, I will plot the data in Excel. I tracked call back and they run on main thread, too. but I still get COMException 0x800AC472, googled and it seems this is a multi-thread issue.

但是我很困惑为什么会这样. 我认为只有一个主线程,并且由于所有回调都在主线程上运行,没有理由有例外吗?

but I am confused why this happened. I think there is only one main thread and since all callback are run on main thread and there is no reason to have the exception?

在主UI线程上,单击功能区/按钮,它将调用Web服务BuildMetaData, 一旦返回,在其回调MetaDataCompleteCallback中,将发送另一个Web服务调用 一旦返回,在其回调DataRequestJobFinished中,它将调用plot以在Excel上绘制数据.见下文

On main UI thread, ribbon/button is clicked, it will call web service BuildMetaData, once it is returned back, in its callback MetaDataCompleteCallback, another web service call is sent Once it is returned back, in its callback DataRequestJobFinished, it will call plot to plot data on Excel. see below

On Main UI class:
Btn_Click()
{
...
                       _reportObjs[index].GenerateReport();

}

关于要生成报告的类

public void GenerateReport()
{
                Request.ParseFunction();
                Request.MetacompleteCallBack = MetaDataCompleteCallback;
                Request.BuildMetaData();
}

public void MetaDataCompleteCallback(int id)
{
            try
            {
                if (Request.IsRequestCancelled)
                {
                    Request.FormulaCell.Dispose();
                    return;
                }

                ErrorMessage = Request.ErrorMessage;
                if (string.IsNullOrEmpty(Request.ErrorMessage))
                {
                    _queryJob = new DataQueryJob(UnityContainer, Request.BuildQueryString(), DataRequestJobFinished, Request);
                }
                else
                {
                    ModifyCommentOnFormulaCellPublishRefreshEvent();
                }
            }
            catch (Exception ex)
            {
                ErrorMessage = ex.Message;
                ModifyCommentOnFormulaCellPublishRefreshEvent();
            }
            finally
            {
                Request.MetacompleteCallBack = null;
            }
} 


        public void DataRequestJobFinished(DataRequestResponse response)
        {
            Dispatcher.Invoke(new Action<DataRequestResponse>(DataRequestJobFinishedUI), response);
        }

        public void DataRequestJobFinished(DataRequestResponse response)
        {
            try
            {
                if (Request.IsRequestCancelled)
                {
                    return;
                }

                if (response.status != Status.COMPLETE)
                {
                    ErrorMessage = ManipulateStatusMsg(response);
                }
                else // COMPLETE
                {
                    // TODO: Convert this into factory pattern
                    var tmpReq = Request as DataRequest;
                    if (tmpReq == null) return;

                    new VerticalTemplate(tmpReq, response, IsOffice2003).Plot();

                }
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;
                MIMICShared.Helper.LogError(e);
            }
            finally
            {
                //if (token != null)
                //    this.UnityContainer.Resolve<IEventAggregator>().GetEvent<DataQueryJobComplete>().Unsubscribe(token);
                ModifyCommentOnFormulaCellPublishRefreshEvent();
                Request.FormulaCell.Dispose();
            }
        }


        on plot class

        public void Plot()
        {
        ... 
           attributeRange.Value2 = headerArray;
           DataRange.Value2 = ....
           DataRange.NumberFormat = ... 
        }

推荐答案

我看到了这个stackoverflow.com/questions/5246288/errormessage-in-excel,social.msdn.microsoft.com/forums/en-US/vsto/线程/…除了等待/重试,似乎没有其他解决方案. 本文讨论如何检查Excel是否处于编辑状态. http: //www.add-in-express.com/creating-addins-blog/2011/03/23/excel-check-user-edit-cell/

I saw this stackoverflow.com/questions/5246288/errormessage-in-excel, social.msdn.microsoft.com/forums/en-US/vsto/thread/… It seems there is no solution to the issue except wait/retry. THis post talks about how to check if Excel is in edit. http://www.add-in-express.com/creating-addins-blog/2011/03/23/excel-check-user-edit-cell/

这篇关于WPF应用程序主线程上的COMException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆