Search code examples
c#asynchronouscallbackmyob

callback based async method with multiple parameters to awaitabletask


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?


Solution

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