WP8 TaskCompletionSource not getting Result

这一生的挚爱 提交于 2020-01-14 06:31:10

问题


I've an extension method for WebClient (WP8)

public static Task<string> DownloadStringTask(this WebClient webClient, Uri uri)
    {
    var tcs = new TaskCompletionSource<string>();

    webClient.DownloadStringCompleted += (s, e) =>
        {
            if (e.Error != null)
            {
                tcs.TrySetException(e.Error);
            }
            else if (e.Cancelled)
            {
                tcs.TrySetCanceled();
            }
            else
            {
                tcs.TrySetResult(e.Result);
            }
        };

    webClient.DownloadStringAsync(uri);

    return tcs.Task;
}

and the call to this method

public string GetResult()
{
    var task = new WebClient().DownloadStringTask(new Uri("http:\\www.foo.com"));

    return task.Result;
}

The DownloadStringCompleted is never executed and obviously there is no result, if I press the pause button on VS always is waiting in task.Result.

Any idea?

Thanks in advance.


回答1:


Is GetResult executed from the main thread? In that case, it may be a deadlock. If I remember correctly, the callback of the WebClient is executed on the main thread, which cannot happen since you're blocking it by calling task.Result.

You have multiple ways to prevent this issue:

  • Use a HttpWebRequest instead of a WebClient

  • Call GetResult from another thread

  • Execute the task asynchronously by using task.ContinueWith instead of directly task.Result

  • Rewrite your method using async/await keywords




回答2:


task.Result blocks the executing thread until the result is available. For me it seems that your code just defeats the purpose of making the request asynchronous. As KooKiz mentioned, use some truly asynchronous API to get the result, such as task.ContinueWith or await task.



来源:https://stackoverflow.com/questions/16708646/wp8-taskcompletionsource-not-getting-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!