如何从C#中的Async方法读取返回值? [英] How can I read return value from Async method in C#?

查看:768
本文介绍了如何从C#中的Async方法读取返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的async方法如下:

public async Task<List<object>> handleSummaryOfWallets()
{
    string token = giveMeToken("URL AND CREDS");

    Channel channel = new Channel("NANANANA GIROUD", ChannelCredentials.Insecure);

    OMGadminAPI.OMGadminAPIClient client = new OMGadminAPI.OMGadminAPIClient(channel);

    var summaryBalancesParams = new OMGadminAPIGetCurrenciesSummariesParams();

    summaryBalancesParams.AdminAuthTokenSecret = token;
    List<object> summariesCurrenciesOMGadmin = new List<object>();

    using (var call = client.GetCurrenciesSummaries(summaryBalancesParams))
    {
        while (await call.ResponseStream.MoveNext())
        {
            OMGadminAPICurrencySummary currencySummary = call.ResponseStream.Current;
            summariesCurrenciesOMGadmin.Add(currencySummary);
            Console.WriteLine(summariesCurrenciesOMGadmin);
        }
        return summariesCurrenciesOMGadmin;
    }
}

如您所见,

上面的async方法返回对象列表.我将这种方法称为:

As you can see, above async method returns list of objects. I call this method as below:

var listOfBalances = balances.handleSummaryOfWallets().Wait();

它给了我错误:

错误CS0815:无法将void分配给隐式类型的变量

Error CS0815: Cannot assign void to an implicitly-typed variable

从错误中我了解到,这不是调用async方法的正确方法.但是我需要从async获取的数据中读取对象的就绪列表.它的请求-响应,没有真正稳定的流.因此,我只需要为每个请求生成一次此列表.我正在使用 gRPC 框架进行RPC调用.

From the error, I understand that this is not correct way to call async method. But I need to read ready list of objects from async fetched data. Its request-response, no real stable stream. So I need to generate this list only once per request. I'm using gRPC framework for RPC calls.

请帮助我获取此数据并准备使用.

Please help me fetch this data and make ready to use.

推荐答案

The Task.Wait method waits for the Task to complete execution. It returns void. That is the reason why the exception.

现在要克服异常并读取返回值,一种方法是在其他答案和注释中提到的; await通话如下:

Now to overcome the exception and to read the return value, one way is as mentioned in other answer and the comments; await the call as below:

public async void TestAsync()
{
    var listOfBalances = await handleSummaryOfWallets();
}

请注意,您的调用方法现在也应该是async方法.

Note that your calling method should also be async method now.

在代码中调用Wait时,看起来您立即想要结果;您别无选择,不必依赖结果.在这种情况下,您可以选择通过调用Wait来停止async链.但是您需要进行如下更改:

As you are calling Wait in your code, it looks that you want the result immediately; you have nothing else left to do that does not depend on result. In that case, you may choose to stop async chain by calling Wait. But you need to do some changes as below:

public void TestAsync()
{
    var task = handleSummaryOfWallets();//Just call the method which will return the Task<List<object>>.
    task.Wait();//Call Wait on the task. This will hold the execution until complete execution is done.
    var listOfBalances = task.Result;//Task is executed completely. Read the result.
}

请注意,调用方法不再是async.其他注释在代码注释中给出.

Note that calling method is no longer async. Other explanation is given in code-comments.

上述代码的其他简短替代方法如下:

Other short alternative to above code is as below:

public void TestAsync()
{
    var listOfBalances = handleSummaryOfWallets().Result;
}

这篇关于如何从C#中的Async方法读取返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆