简体   繁体   English

转换异步方法以使用任务并行库

[英]Convert an Async method to use Task Parallel Library

Suppose I have the following method signature for an Async method: 假设我为异步方法具有以下方法签名:

public IAsyncResult MyAsyncMethod(AsyncCallback asyncCallback, object state);

And, therefore, I use the following code to call it and assign it a state object and a callback: 因此,我使用以下代码对其进行调用,并为其分配一个state对象和一个回调:

public class test
{
    // Private class to hold / pass-through data used by Async operations
    private class StateData
    {
        //... Whatever properties I'll need passed to my callback ...
    }

    public void RunOnSeparateThread()
    {
        StateData stateData = new StateData { ... };
        MyAsyncMethod(m_asyncCallback, stateData);
    }

    private AsyncCallback m_asyncCallback = new AsyncCallback(AsyncComplete);
    private static void AsyncComplete(IAsyncResult result)
    { 
        // Whatever stuff needs to be done after completion of Async method
        // using data returned in the stateData object
    }
}

I'm looking to do the same thing using Task Parallel Library and am not certain how to do it. 我正在寻找使用任务并行库执行相同的操作,但不确定如何执行。 What would be the correct code to achieve the same result, being: 实现相同结果的正确代码是:

  1. Run MyAsyncMethod on a separate thread 在单独的线程上运行MyAsyncMethod
  2. On completion have a state object returned with it passed to the next method - In this case AsyncComplete 完成后,将返回的state对象传递给下一个方法-在这种情况下,为AsyncComplete

Or is there a better way to accomplish the same result? 还是有更好的方法来实现相同的结果?

Also, although this is written in C#, I'm equally comfortable in VB and C#, so any answer would be greatly appreciated. 另外,尽管这是用C#编写的,但我对VB和C#同样满意,因此,任何答复都将不胜感激。

Thank you and hopefully this makes sense. 谢谢,希望这是有道理的。

Suppose I have the following method signature for an Async method: 假设我为异步方法具有以下方法签名:

 public IAsyncResult MyAsyncMethod(AsyncCallback asyncCallback, object state); 

I would expect the return value to be Task<T> ( IAsyncResult is normally returned from the BeginXYZ when there is a matching EndXYZ in the original .NET async pattern), and not taking a callback. 我期望的返回值是Task<T> IAsyncResult通常从返回BeginXYZ时,有一个匹配EndXYZ在原始.NET异步模式)和未服用的回调。 (As presented it seems to be a bit of one pattern and a bit of another.) (如前所述,这似乎是一种模式,而另一种模式。)

If my expectation is right then, just call the method and use the retuned Task<T> . 如果我的期望是正确的,则只需调用该方法并使用重新调整的Task<T>

If it is really a IAsyncResult then TaskFactory has a helper method: FromAsync which amongst its many overloads can take an IAsyncResult and an Action<IAsyncResult> . 如果确实是IAsyncResultTaskFactory有一个辅助方法: FromAsync ,它的许多重载中都可以采用IAsyncResultAction<IAsyncResult>

So your code becomes: 因此,您的代码变为:

var ia = MyAsyncMethod(() => {}, null);
var t = Task.Factory.FromAsync(ia, x => { 
    // called with ia from MyAsyncMethod on completion.
    //  Can close over any state
});

// ...
var result = (TypeofResult)t.Result;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM