asynchronous和等待命令编译没有错误,但不会让我的Windows窗体响应

我有一个Excel工具箱项目上的工作表。 我有一个下拉菜单,允许用户select一个值,并点击“运行”button(我的button1),并认为所有工作正常,假设数据是在Excel工作表内,目前是手动完成。 但是,如果我想添加第二个button(button2),它将自动从SQL Server中“刷新”数据,它将运行…但不是asynchronous的。 这是我的button2:

private async void button2_Click(object sender, EventArgs e) { var Excel = Globals.ThisWorkbook.Application; var activebook = Excel.ActiveWorkbook; var ws = Excel.ActiveSheet; SQLServer server = new SQLServer(); int t = await Task.Run(() => server.getInfo(activebook, ws, Excel)); } 

代码运行良好,但我不想让用户在Excel窗口响应之前等待6000行excel表单被填充。 我在这里做错了什么?

编辑

我很抱歉的混淆,我对这个网站还很新,并不意味着造成这么多混乱。 我正试图运行这个方法:

  public void getInfo(Workbook book, Worksheet activesheet, Microsoft.Office.Interop.Excel.Application app) { SqlConnection cnn; string connectionstring = "#########"; string sql = null; ////***Opening SQL Database connectionstring = "Data Source=########;Initial Catalog=BrightreeData;Persist Security Info=True;User ID=######;Password=######"; cnn = new SqlConnection(connectionstring); cnn.Open(); activesheet.Range["K" + 1].Value = "Connected"; ////** Write your Sql Query here sql = "SELECT [StopReason], [SOKey], [NickName] FROM [BrightreeData].[dbo].[SalesOrder] Where [StopReason] = 'Ineligible Policy' ORDER BY [SOKey]"; ///*** Preparing to retrieve value from the database SQL.DataTable dtable = new SQL.DataTable(); SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn); SQL.DataSet ds = new SQL.DataSet(); dscmd.Fill(dtable); int count = dtable.Rows.Count; for (int i = 0; i < dtable.Rows.Count; i++) { try { check: activesheet.Range["C" + 1].Value = (i + 1) + " out of " + count + " rows"; soService.DataFetchServiceResponseUsingSalesOrder salesorder = soAction.SalesOrderFetchByBrightreeID(dtable.Rows[i]["SOKey"].ToString()); activesheet.Range["J" + (8 + i)].Value = dtable.Rows[i]["NickName"]; activesheet.Range["A" + (8 + i)].Value = salesorder.Items[0].SalesOrderInsuranceInfo.Payors[1].payorPolicyInfo.Name.ToString(); activesheet.Range["D" + (8 + i)].Value = salesorder.Items[0].SalesOrderInsuranceInfo.Payors[1].payorPolicyInfo.PolicyNumber; //If the current row has the same policy as the last row, it is a repeat and nothing worth grabbing that data. // // Eventually add something at the end of the program that deletes rows that are blank?...for now I will manually delete empty rows. if(activesheet.Range["D" + (8 + i)].Value == activesheet.Range["D" + (7 + i)].Value & activesheet.Range["D" + (8 + i)].Text != "") { activesheet.Range["C" + (8 + i)].Value = "Repeat Patient"; count--; i++; goto check; } activesheet.Range["J" + (8 + i)].Value = "St Lukes"; activesheet.Range["K" + (8 + i)].Value = System.DateTime.Today.Date; string ptID = Convert.ToString(salesorder.Items[0].SalesOrderClinicalInfo.Patient.BrightreeID); ptService.DataFetchServiceResponseUsingPatient ptSearch = ptAction.PatientFetchByBrightreeID(ptID); activesheet.Range["B" + (8 + i)].Value = ptSearch.Items[0].PatientGeneralInfo.Name.First; activesheet.Range["C" + (8 + i)].Value = ptSearch.Items[0].PatientGeneralInfo.Name.Last; activesheet.Range["E" + (8 + i)].Value = ptSearch.Items[0].PatientGeneralInfo.BirthDate; activesheet.Range["L" + (8 + i)].Value = ptSearch.Items[0].PatientGeneralInfo.PtID; }catch { activesheet.Range["B" + (8 + i)].Value = "Error getting patient information"; } } } 

使用这个调用:

 private async void button2_Click(object sender, EventArgs e) { var Excel = Globals.ThisWorkbook.Application; var activebook = Excel.ActiveWorkbook; var ws = Excel.ActiveSheet; SQLServer server = new SQLServer(); await Task.Run(() => server.getInfo(activebook, ws, Excel)); } 

在我的button2_Click()方法的最后一行发生错误。

  An exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Core.dll but was not handled in user code Additional information: Cannot implicitly convert type 'type' to 'object' 

尝试这个:

 await Task.Run((Action)(() => server.getInfo(activebook, ws, Excel))); 

出于某种原因(可能是因为你传递的参数是COM对象),系统会将这些参数视为dynamic的,并且会错误地认为你传递的委托会返回一个值。

如果您将鼠标移到Visual Studio中的Task.Run上,您应该看到系统正试图调用这个重载:

 Task<TResult> Run<TResult>(Func<TResult> function) 

TResultdynamic