如何使一个asynchronous方法返回一个值?

我知道如何使asynchronous方法,但说我有一个方法,做了很多工作,然后返回一个布尔值?

如何返回callback布尔值?

澄清

public bool Foo(){ Thread.Sleep(100000); // Do work return true; } 

我想能够使这个asynchronous。

有几种方法可以做到这一点…最简单的方法是使asynchronous方法也可以执行后续操作。 另一种stream行的方法是通过callback,即

 void RunFooAsync(..., Action<bool> callback) { // do some stuff bool result = ... if(callback != null) callback(result); } 

另一种方法是在asynchronous操作完成时引发一个事件(结果是事件参数数据)。

另外,如果您正在使用TPL,则可以使用ContinueWith

 Task<bool> outerTask = ...; outerTask.ContinueWith(task => { bool result = task.Result; // do something with that }); 

从C#5.0开始 ,您可以指定方法为

 public async Task<bool> doAsyncOperation() { // do work return true; } bool result = await doAsyncOperation(); 

可能最简单的方法是创build一个委托,然后BeginInvoke ,然后在将来的某个时间等待一个EndInvoke

 public bool Foo(){ Thread.Sleep(100000); // Do work return true; } public SomeMethod() { var fooCaller = new Func<bool>(Foo); // Call the method asynchronously var asyncResult = fooCaller.BeginInvoke(null, null); // Potentially do other work while the asynchronous method is executing. // Finally, wait for result asyncResult.AsyncWaitHandle.WaitOne(); bool fooResult = fooCaller.EndInvoke(asyncResult); Console.WriteLine("Foo returned {0}", fooResult); } 

使用BackgroundWorker。 它将允许您完成callback,并允许您跟踪进度。 您可以将事件参数的结果值设置为结果值。

  public void UseBackgroundWorker() { var worker = new BackgroundWorker(); worker.DoWork += DoWork; worker.RunWorkerCompleted += WorkDone; worker.RunWorkerAsync("input"); } public void DoWork(object sender, DoWorkEventArgs e) { e.Result = e.Argument.Equals("input"); Thread.Sleep(1000); } public void WorkDone(object sender, RunWorkerCompletedEventArgs e) { var result = (bool) e.Result; } 

也许你可以尝试BeginInvoke一个委托指向你的方法是这样的:

delegate string SynchOperation(string value); class Program { static void Main(string[] args) { BeginTheSynchronousOperation(CallbackOperation, "my value"); Console.ReadLine(); } static void BeginTheSynchronousOperation(AsyncCallback callback, string value) { SynchOperation op = new SynchOperation(SynchronousOperation); op.BeginInvoke(value, callback, op); } static string SynchronousOperation(string value) { Thread.Sleep(10000); return value; } static void CallbackOperation(IAsyncResult result) { // get your delegate var ar = result.AsyncState as SynchOperation; // end invoke and get value var returned = ar.EndInvoke(result); Console.WriteLine(returned); } }
delegate string SynchOperation(string value); class Program { static void Main(string[] args) { BeginTheSynchronousOperation(CallbackOperation, "my value"); Console.ReadLine(); } static void BeginTheSynchronousOperation(AsyncCallback callback, string value) { SynchOperation op = new SynchOperation(SynchronousOperation); op.BeginInvoke(value, callback, op); } static string SynchronousOperation(string value) { Thread.Sleep(10000); return value; } static void CallbackOperation(IAsyncResult result) { // get your delegate var ar = result.AsyncState as SynchOperation; // end invoke and get value var returned = ar.EndInvoke(result); Console.WriteLine(returned); } } 

然后使用您作为AsyncCallback发送的方法中的值继续。

您应该使用asynchronous方法的EndXXX来返回值。 EndXXX应该等到使用IAsyncResult的WaitHandle结果并返回值。