gpt4 book ai didi

c# - Async 和 Await 命令编译没有错误,但不会让我的 Windows 窗体响应

转载 作者:太空宇宙 更新时间:2023-11-03 19:54:52 24 4
gpt4 key购买 nike

<分区>

我有一张 Excel 工作表,上面有各种工具箱项目。我有一个下拉菜单,允许用户选择一个值并点击“运行”按钮(我的 button1)并且一切正常,假设数据在 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'

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com