Async 和 Await 命令编译没有错误,但不会让我的 Windows 窗体响应
本文关键字:我的 Windows 响应 窗体 Await 命令 编译 有错误 Async | 更新日期: 2023-09-27 18:30:53
我有一个Excel工作表,上面有各种工具箱项目。 我有一个下拉菜单,允许用户选择一个值并点击"运行"按钮(我的按钮1),并且假设数据在 Excel 工作表中并且目前是手动完成的,一切正常。 但是,如果我想添加第二个按钮(button2)来自动从SQL Server"刷新"数据,它会运行...但不是异步的。 这是我的按钮2:
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 对象),系统会将参数和方法调用视为动态的,并且会错误地认为您传递的委托将返回值。
如果在 Visual Studio 中将鼠标移到Task.Run
上,应会看到系统正在尝试调用此重载:
Task<TResult> Run<TResult>(Func<TResult> function)
TResult
在哪里dynamic
.