callback based async method with multiple paramete

2019-09-11 06:50发布

问题:

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:

As mentioned in the comment

The SDK for Accountright API supports async/await i.e. GetRangeAsync

so you can do something like this if you wanted/needed to 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;
}