簡體   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