使用await关键字

时间:2015-11-25 16:14:34

标签: c# asp.net .net asp.net-mvc

我创建了一个新的ASP.Net 5应用程序,并且在生成的代码中有很多使用await关键字,例如:

var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
    await _signInManager.SignInAsync(user, isPersistent: false);
    return RedirectToAction(nameof(HomeController.Index), "Home");
}

我的问题是我们通过这个完成了什么?我们仍然需要等待获取结果,我们无法继续进行直到我们得不到结果。那么await在这里有什么意义呢?

2 个答案:

答案 0 :(得分:1)

优点是控件返回给当前方法的调用者。您的异步方法将"恢复"当等待的任务完成时。

这非常有用,因为它不需要管理线程和/或回调。

编辑:

这就是为什么你想要(或不想)使用async / await的原因。我们假设你有这样的网络电话:

import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Window 2.1

    Rectangle {
        id: modellerDatapane

        property double factor : 1.5

        ScrollView {
            id: scrollCanvas
            anchors.fill: parent

            Flickable{
                id: flickableCanvas
                anchors.fill: parent
                contentWidth: canvas.width
                contentHeight: canvas.height

                Canvas {
                    Rectangle{
                        border.width: 20
                        color: "transparent"
                        border.color: "red"
                        anchors.fill:parent
                    }

                    property bool fill: true
                    property bool stroke: true
                    property real alpha: 1.0
                    property real scale : 5
                    property real rotate : 5
                    antialiasing: true

                    id: canvas
                    height: 5000
                    width: 5000

                    state : "visible"
                    transform : Scale{
                        id : tform
                    }

                    onPaint: {
                        console.log("painted")
                        var ctx = canvas.getContext('2d');
                        var originX = 100
                        var originY = 100
                        ctx.save();
                        ctx.clearRect(0, 0, 500, 500);
                        ctx.translate(originX, originX);
                        ctx.globalAlpha = 1;
                        ctx.strokeStyle = "blue";
                        ctx.fillStyle = "yellow";
                        ctx.lineWidth = 3;

                        ctx.translate(originX, originY)
                        ctx.scale(tform.xScale, tform.yScale);
                        ctx.rotate(1);
                        ctx.translate(-originX, -originY)

                        ctx.beginPath();
                        ctx.moveTo(75,40);
                        ctx.bezierCurveTo(75,37,70,25,50,25);
                        ctx.bezierCurveTo(20,25,20,62.5,20,62.5);
                        ctx.bezierCurveTo(20,80,40,102,75,120);
                        ctx.bezierCurveTo(110,102,130,80,130,62.5);
                        ctx.bezierCurveTo(130,62.5,130,25,100,25);
                        ctx.bezierCurveTo(85,25,75,37,75,40);
                        ctx.closePath();
                        if (canvas.fill)
                            ctx.fill();
                        if (canvas.stroke)
                            ctx.stroke();
                        ctx.restore();

                    }


                    MouseArea {
                        parent: scrollCanvas
                        anchors.fill: canvas
                        propagateComposedEvents: true

                        onWheel: {
                            if(wheel.angleDelta.y > 0){  // zoom in
                                canvasZoomIn();
                            }
                            else{                        // zoom out
                                canvasZoomOut();
                            }
                        }
                        onPressed:{
                            mouse.accepted = false
                        }
                        onPositionChanged:{mouse.accepted = false}
                        onReleased:{mouse.accepted = false}
                    }
                }

            }
        }

        function canvasZoomOut(){
            scaleCanvas(1/factor);

        }

        function canvasZoomIn(){
            scaleCanvas(factor);

        }

        function scaleCanvas(scaleFactor){
            tform.xScale *=scaleFactor;
            tform.yScale *=scaleFactor;
            canvas.height *= scaleFactor;
            canvas.width *= scaleFactor;
        }

    }

这是一个阻塞调用,它会停止当前正在执行的线程并等待调用完成。 public RequestResult Request(string queryString) { var response = connection.Request(queryString); } 方法可能有Async版本,通常以Request(string)为后缀。如果是这样,您可以Async方法await使用它。

async Task

控件将被释放,直到任务完成,允许调用者继续。这对GUI应用程序非常有用。您可以将public RequestResult Request(string queryString) { var response = connection.Request(queryString); } 方法绑定到事件,将其触发,启动请求并在请求完成时保持UI更新。

async之前,首选方法是使用async/await方法。您可以将委托传递给Begin方法,并在任务完成时调用它。我想指出,这仍然可以使用! Here是有关异步模式的更多详细信息。 Begin/End称为TAP,基于任务的异步编程。

编辑:C#编译器将在async/await关键字之后采用该方法的其余部分,并使其成为回调(内部)。

答案 1 :(得分:0)

据推测,所有这些都发生在具有async关键字的函数中。考虑的调用功能 - 他们还 await某事(这个功能!)他们很可能继续做其他事情在此期间的事情。

在ASP.NET的上下文中,应用程序在堆栈中捕获传入请求并调用控制器上的方法以计算要作为响应发送的内容。如果您的控制器操作为async,则您的应用程序可以执行多任务(惩罚意图...)并更快地提供更多请求,而无需等待例如数据库或I / O.

相关问题