callback based async method with multiple parameters to awaitabletask

纵然是瞬间 提交于 2019-12-12 03:39:20

问题


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;
}


来源:https://stackoverflow.com/questions/32341172/callback-based-async-method-with-multiple-parameters-to-awaitabletask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!