-1

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 = ... 
        }
toosensitive
  • 2,335
  • 7
  • 46
  • 88
  • 1
    How do you know all the callbacks are run on the main thread? What kind of callbacks do you get, and how do you serialize them to the main thread? Do you use a dispatcher? BeginInvoke? – EFraim Dec 19 '11 at 19:14
  • thanks. I step in Visual Studio and see they are run on mainThread. – toosensitive Dec 19 '11 at 19:31
  • I called method A when a UI button/ribbon is clicked. in the method, I will call web servcie asynchronously with callback. In callback, I will plot – toosensitive Dec 19 '11 at 19:32
  • 1
    Post the code you use to marshal the callback to the main thread. – Hans Passant Dec 19 '11 at 19:38
  • Thanks, Hans. I just edit my post to include code sample. – toosensitive Dec 19 '11 at 20:09
  • I saw this http://stackoverflow.com/questions/5246288/errormessage-in-excel, http://social.msdn.microsoft.com/forums/en-US/vsto/thread/9168f9f2-e5bc-4535-8d7d-4e374ab8ff09/ It seems there is no solution to the issue except wait/retry – toosensitive Apr 13 '12 at 21:28

1 Answers1

-2

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/

toosensitive
  • 2,335
  • 7
  • 46
  • 88