COMException on Main Thread of WPF application

走远了吗. 提交于 2020-01-07 16:35:12

问题


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?

Edit: 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();

}

on Class to 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 = ... 
        }

回答1:


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/



来源:https://stackoverflow.com/questions/8566214/comexception-on-main-thread-of-wpf-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!