将变量传递给异步调用方法的结果

时间:2018-03-26 16:17:41

标签: wcf asynchronous

Net 4.0 App中有DataGrid个数据。对于选定的行,我获取其中一列的值并将其传递给WCF服务的异步方法。这个方法可以传递这个值吗?

btn_Click(object sender, RoutedEventArgs e) {
    DataRowView rv = (DataRowView)dgData.SelectedItem;
    rv["TimeBeg"] = DateTime.Now.ToString("h:mm:ss");
    string val=rv["Id"].ToString();
    srAsync.ServClient clP = new srAsync.ServClient();
    clP.MethodCompleted += cl_MethodComplete;
    clP.MethodAsync(val);
}

在调用之后,用户可以选择另一个DataGrid项,并为它们调用Async方法,但在完整方法中,我需要使用此值调用另一个方法并更新行{{1 }}

DataGrid

1 个答案:

答案 0 :(得分:1)

我要做的是使用lambda函数将用户点击的行传递给处理程序:

btn_Click(object sender, RoutedEventArgs e) {
            DataRowView rv = (DataRowView)dgData.SelectedItem;
            rv["TimeBeg"] = DateTime.Now.ToString("h:mm:ss");
            string val = rv["Id"].ToString();
            srAsync.ServClient clP = new srAsync.ServClient();
            clP.MethodCompleted += (currentSender, currentE) => cl_MethodComplete(currentSender, currentE, rv);
            clP.MethodAsync(val);
}

当然,您必须向cl_MethodComplete添加另一个参数,该参数将能够在原始行上运行:

private void cl_MethodComplete(object sender, srA.MethodCompletedEventArgs e, DataRowView originalRow) {
    originalRow["TimeEnd"] = DateTime.Now.ToString("h:mm:ss");
    sr.ServClient clP = new sr.ServClient();
    clP.AnotherMethod(val);
    ...