基于回调的异步方法,具有多个参数到 awaitabletask

callback based async method with multiple parameters to awaitabletask

我有以下代码连接MYOB的SDK

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

其中

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

我想将其转换为使用 TaskCompletionSource 喜欢 this example

但是我的 OnComplete 有多个参数。 我该如何编码?

如评论中所述

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

所以如果你 wanted/needed 到 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;
}