none
Problem with async/await, Excel events in Excel Application Level Add-in RRS feed

  • Question

  • I faced a problem with using async /await in my add-in application, I have custom task pane and user can download some data with it, so there are problems:

    1. After awaited operation completed the control is not returned to previous execution context. I created example with windows forms and paste the same method with awaiting and it works fine - I can use form controls after await operation, but not in add-in, I have to call custom task pane Invoke method.

    2. In my add-in I need to handle some events - worksheet changed, worksheet activated/deactivated, but these events disappear and isn't fired after await operation and I have to use something like "toggling"(remove+add) events after each await using.

    So maybe vsto framework can not support new features(as async await) of last versions .NET Framework?  

    • Moved by CoolDadTx Wednesday, September 30, 2015 2:14 PM Office related
    Wednesday, September 30, 2015 11:31 AM

Answers

  • Hi,

    >>1. After awaited operation completed the control is not returned to previous execution context. I created example with windows forms and paste the same method with awaiting and it works fine - I can use form controls after await operation, but not in add-in, I have to call custom task pane Invoke method.<<

    Since we have use async/await, it will create a new thread to execute the task and before the result returned, we can change the execution context. The issue is expected, would you mind sharing a code sample to help us narrow down this issue?

    >>2. In my add-in I need to handle some events - worksheet changed, worksheet activated/deactivated, but these events disappear and isn't fired after await operation and I have to use something like "toggling"(remove+add) events after each await using. <<

    As far as I test, the event works well when I using the await key words. Here is an example that there are two buttons. First buttons will add worksheet active event for every spreadsheet, and the second button will get the text from website and using await to set the text to another worksheet.

       private async void button4_Click(object sender, EventArgs e)
            {
                PrintButtonText();
                button4.Text = "new Text";
                string text = await DoSomething();
                PrintButtonText();
            }
     private async Task<string>  DoSomething()
            {
                HttpClient client = new HttpClient();
                Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");
    
                string urlContents = await getStringTask;
                Globals.ThisAddIn.Application.ActiveCell.Value = urlContents;
                Globals.ThisAddIn.Application.Worksheets[2].Activate();
                return urlContents;
            }
    
            private void button5_Click(object sender, EventArgs e)
            {
    
                foreach (Worksheet ws in Globals.ThisAddIn.Application.Worksheets)
                {
                    Microsoft.Office.Tools.Excel.Worksheet wsVSTO = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveSheet);
                    wsVSTO.ActivateEvent += Ws_ActivateEvent;
                }
            }
    
            private void Ws_ActivateEvent()
            {
                MessageBox.Show("Ws_ActivateEvent");
            }

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 2, 2015 2:55 AM
    Moderator