繁体   English   中英

异步任务 <TResult> - 返回值

[英]async Task<TResult> - Return Value

如何使用“ async Task<string> ”从方法返回数据。 我尝试按照以下链接使用, 如何处理异步函数中的返回值任何人都可以提供答案吗?


使用的方法,

Public Class module
{
      private static async Task<string> a<T>(string x1, object file1)
      {
            HttpResponseMessage x;
            x = await b.doGet(function);
            string ret = await x.Content.ReadAsStringAsync();
            return ret; //JSON Data
      }
}

调用,

public string get()
{
      Task<string> cnt = module.a<string>(x, file());
      MessageBox.Show(cnt.Result); // Loading, but not showing the result
}

谢谢Dinesh

您的签名是正确的,并返回Task<string>

这笔交易是当你打电话给a ,你有两种方法来获取string

public string get()    // For option 2 say public async Task<string> get() 
{
    //Option 1 - Using Task<string>
    Task<string> cnt = module.a<string>(x, file()); // or var cnt = ...
    MessageBox.Show(cnt.GetAwaiter().GetResult()); // Return the string you want

    //Option 2 - Using await
    MessageBox.Show(await module.a<string>(x, file())); // Return the string you want
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM