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

标签 c# excel winforms asynchronous

<分区>

我有一张 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'

最佳答案

试试这个:

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

出于某种原因(可能是因为您传递的参数是 COM 对象),系统将参数和方法调用视为动态的,并且会错误地认为您传递的委托(delegate)将返回一个值。

如果在 visual studio 中将鼠标移到 Task.Run 上,您应该会看到系统正在尝试调用此重载:

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

其中 TResult动态

关于c# - Async 和 Await 命令编译没有错误,但不会让我的 Windows 窗体响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34857263/

相关文章:

vba - 使用 WeekdayName 函数获取上周一

enums - 如何在 Excel UDF 中使用枚举标识符

winforms - 使用 WinForms 显示带有大量 ComboBox 控件的选项卡很慢

c# - 用于 Windows Azure 服务管理的 Metro 应用程序中的客户端证书

c# - 如何将 View 模型的 caliburn.micro 绑定(bind)到组合框选择的值?

c# - C# 中线段与轴对齐框的交点

c# - 模态进度表显示 IProgress 并支持取消 WinForms 的异步任务

c# - 打开默认邮件客户端和附件

vba - 刷新 Excel 连接

c# - Visual C# - 如何对齐 winforms 中的项目?