工作完成后如何做某事(IAsyncResult)

时间:2011-05-24 09:08:43

标签: c# winforms

ProgressForm是一个表格。 我想在ProccessingReport方法执行完成后做一些事情。

IAsyncResult AsyncResult = ProgressForm.BeginInvoke(new Action(ProcessingReport));

现在我做这样的事情:

    while (!AsyncResult.IsCompleted)
    {
        Application.DoEvents();
    }
    doSomething();

我无法将doSomething()操作移动到ProcessingReport方法。有可能吗?谢谢。

1 个答案:

答案 0 :(得分:1)

创建一个DoAsyncProcessingReportResult方法,该方法采用如下参数:

private void DoAsyncProcessingReportResult(IAsyncResult iar)
{
    AsyncResult ar = (AsyncResult)iar;
    // map this to your delegate (take mine as an example)
    Func<RemoteDirectoryInfo> LoadProcessingReport =
          (Func<RemoteDirectoryInfo>)ar.AsyncDelegate;

    // Then get your answer from calling the EndInvoke
    _Directory = LoadProcessingReport.EndInvoke(ar);
}

你打电话给ProcessingReport(以我的为例)

Func<RemoteDirectoryInfo> LoadProcessingReport = ProcessingReport;
LoadProcessingReport.BeginInvoke(DoAsyncProcessingReportResult, null);

我修改了我的代码,以便我能够确定最好的代码。您必须修复委托声明以适合您的代码。