callback based async method with multiple parameters to awaitabletask

时间:2015-09-01 21:26:55

标签: c# asynchronous callback myob

I have the following code to connect to MYOB's SDK

    var cfsCloud = new CompanyFileService(_configurationCloud, null, _oAuthKeyService);
    cfsCloud.GetRange(OnComplete, OnError);

where

private  void OnComplete(HttpStatusCode statusCode, CompanyFile[] companyFiles)
    {  // ask for credentials etc }

I want to convert this to use a TaskCompletionSource like this example

however my OnComplete has multiple parameters. How do I code that?

1 个答案:

答案 0 :(得分:2)

如评论中所述

Accountright API的SDK支持async / await,即GetRangeAsync

所以如果你想要/需要wrap it in a TaskCompletionSource

,你可以这样做
static Task<CompanyFile[]> DoWork()
{
    var tcs = new TaskCompletionSource<CompanyFile[]>();
    Task.Run(async () =>
    {
        var cfsCloud = new CompanyFileService(_configurationCloud, null, _oAuthKeyService);
        var files = await cfsCloud.GetRangeAsync();
        tcs.SetResult(files);
    });
    return tcs.Task;
}