无法使Task.WaitAll工作

时间:2017-11-02 19:48:02

标签: asynchronous xamarin.forms task wait

我试图让Task.WaitAll工作,但我的代码永远不会超过WaitAll代码行。这很简单。 On Appearing主页的事件尝试对Web API进行几次调用。关于我做错了什么的任何建议/想法?

以下是我页面的OnAppearing事件中的代码

   protected override void OnAppearing()
    {
        try
        {
            base.OnAppearing();
            List<Task<String>> lobj_TaskList = new List<Task<String>>();
            WebAPICaller lobj_WebAPICaller = new WebAPICaller();
            Debug.WriteLine("Process StartUp");

            lobj_TaskList.Add(lobj_WebAPICaller.HTTPGetWebServiceAsync("/DOW/EN"));
            lobj_TaskList.Add(lobj_WebAPICaller.HTTPGetWebServiceAsync("/languages/true"));

            Task.WaitAll(lobj_TaskList.ToArray());
        }
        catch (Exception ex)
        {
            App.ProcessException(ex);
        }
    }

这是WebAPICaller类

using System;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace TestWebAPI
{
public class WebAPICaller : IDisposable

{
    HttpClient iobj_HTTPClient = null;
    public void Dispose()
    {
        if (iobj_HTTPClient != null)
            iobj_HTTPClient.Dispose();

    }

    public async Task<string> HTTPGetWebServiceAsync(string ps_URI)
    {

        string ls_Response = "";
        string ls_JSONData = "";
        string ls_Prefix = "";

        try
        {
            iobj_HTTPClient = await GetClient();

            switch (Device.RuntimePlatform)
            {
                case Device.Android:
                    ls_Prefix = App.APIStandardPrefix;
                    break;
                default:
                    ls_Prefix = App.APISecurePrefix;
                    break;
            }

            Debug.WriteLine("before api call");
            iobj_HTTPClient.BaseAddress = new Uri(ls_Prefix);
            ls_JSONData = await iobj_HTTPClient.GetStringAsync(ps_URI);
            Debug.WriteLine("after api call");
            ls_Response = System.Net.WebUtility.HtmlDecode(ls_JSONData);

        }
        catch (Exception ex)
        {
            Debug.WriteLine("api call error");
            App.ProcessException(ex);
        }
        return ls_Response;

    } 

    private async Task<HttpClient> GetClient()
    {
        HttpClient lobj_HTTPClient = null;

        if (lobj_HTTPClient == null)
        {
            lobj_HTTPClient = new HttpClient();
            lobj_HTTPClient.DefaultRequestHeaders.Add("Accept", "application/json");
            lobj_HTTPClient.MaxResponseContentBufferSize = 2147483647;
            lobj_HTTPClient.Timeout = new TimeSpan(0, 0, 0, 0, 60000);
        }

        return lobj_HTTPClient;
    }

}
}

1 个答案:

答案 0 :(得分:1)

使用 Task.WhenAll 代替 Task.WaitAll

WhenAll将等待所有任务完成。 await链将在第一个异常中止,但是仍在等待执行未等待的任务。这会导致意外的并发。

看看下面的代码,它会对你有帮助。

protected async override void OnAppearing()
{
    try
    {
        base.OnAppearing();
        List<Task<String>> lobj_TaskList = new List<Task<String>>();
        WebAPICaller lobj_WebAPICaller = new WebAPICaller();
        Debug.WriteLine("Process StartUp");

        lobj_TaskList.Add(lobj_WebAPICaller.HTTPGetWebServiceAsync("/DOW/EN"));
        lobj_TaskList.Add(lobj_WebAPICaller.HTTPGetWebServiceAsync("/languages/true"));

        await Task.WhenAll(lobj_TaskList.ToArray());
    }
    catch (Exception ex)
    {
        App.ProcessException(ex);
    }
}