相关疑难解决方法(0)

多个等待与Task.WaitAll - 等效?

在性能方面,会这2种方法运行GetAllWidgets()GetAllFoos()并行?

有没有理由使用一个而不是另一个?在编译器的幕后似乎发生了很多事情,所以我觉得不清楚.

=============方法A:使用多个等待======================

public async Task<IHttpActionResult> MethodA()
{
    var customer = new Customer();

    customer.Widgets = await _widgetService.GetAllWidgets();
    customer.Foos = await _fooService.GetAllFoos();

    return Ok(customer);
}
Run Code Online (Sandbox Code Playgroud)

===============方法B:使用Task.WaitAll =====================

public async Task<IHttpActionResult> MethodB()
{
    var customer = new Customer();

    var getAllWidgetsTask = _widgetService.GetAllWidgets();
    var getAllFoosTask = _fooService.GetAllFos();

    Task.WaitAll(new List[] {getAllWidgetsTask, getAllFoosTask});

    customer.Widgets = getAllWidgetsTask.Result;
    customer.Foos = getAllFoosTask.Result;

    return Ok(customer);
}
Run Code Online (Sandbox Code Playgroud)

=====================================

.net c# task-parallel-library async-await

48
推荐指数
2
解决办法
2万
查看次数

异步等待处理程序死锁

我陷入了异步死锁,我无法找出正确的语法来解决它.我已经看了几个不同的解决方案,但似乎无法弄清楚导致问题的原因.

我使用Parse作为后端并尝试使用处理程序写入表.我的处理程序看起来像:

public class VisitorSignupHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        //Get the user's name and email address
        var UserFullName = context.Request.QueryString["name"].UrlDecode();
        var UserEmailAddress = context.Request.QueryString["email"].UrlDecode();

        //Save the user's information
        var TaskToken = UserSignup.SaveUserSignup(UserFullName, UserEmailAddress);
        TaskToken.Wait();
        ....

    }

    public bool IsReusable { get { return false; } }
}
Run Code Online (Sandbox Code Playgroud)

然后它调用我的中间层:

public static class UserSignup
{
    public static async Task SaveUserSignup(string fullName, string emailAddress)
    {
        //Initialize the Parse client with the Application ID and the Windows key …
Run Code Online (Sandbox Code Playgroud)

.net ihttphandler async-await parse-platform

7
推荐指数
1
解决办法
2343
查看次数