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:
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. Simple example:
private void SearchPaneButton_Click(object sender, EventArgs e) { Search(); } private async void Search() { var searchText = SearchTextBox.Text; SearchPaneButton.Text = "Loading…"; var data = await new DataServiceClient().GetDataAsync(searchText); this.Invoke((MethodInvoker)(() => { SearchPaneButton.Text = "Search"; ToggleWorkbookEvents(); })); }
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. Simple example:
private void ToggleWorkbookEvents() { var excel = Globals.MyAddIn.Application; //need to ensure event will not be set twice excel.SheetActivate -= CheckSheetForData; excel.SheetActivate += CheckSheetForData; if (excel.ActiveWorkbook != null) { var activeSheet = Globals.MyAddIn.GetActiveWorksheet(); //need to ensure event will not be set twice activeSheet.Change -= Worksheet_Changed; activeSheet.Change += Worksheet_Changed; } }
So maybe vsto framework can not support new features(like async await) of last versions .NET Framework?
Search
? – HighkeyedSearch
method in the click event handler, just invoke – TopdressSearch
method for example), i just want to show that I had to useMethodInvoker
andSearch
method is called from button click event. – Topdress