WPF应用程序主线程出现COMException
本文关键字:COMException 线程 应用程序 WPF | 更新日期: 2023-09-27 18:18:14
WPF、Excel插件、c#、我有多个异步调用从主线程上的web服务获取数据,然后在回调,我会在Excel中绘制数据。我跟踪了回调,它们也在主线程上运行。但我仍然得到COMException 0x800AC472,谷歌,似乎这是一个多线程问题。
但我很困惑为什么会发生这种情况。我认为只有一个主线程,因为所有回调都在主线程上运行,没有理由有异常?
编辑:在主UI线程上,ribbon/button被点击,它会调用web service BuildMetaData,一旦它被返回,在它的回调MetaDataCompleteCallback中,另一个web服务调用被发送一旦它被返回,在它的回调DataRequestJobFinished中,它将调用plot在Excel上绘制数据。见下文
On Main UI class:
Btn_Click()
{
...
_reportObjs[index].GenerateReport();
}
关于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/thread/…似乎没有解决这个问题,除了等待/重试。这篇文章讨论了如何检查Excel是否在编辑中。http://www.add-in-express.com/creating-addins-blog/2011/03/23/excel-check-user-edit-cell/