我不确定我对Await的理解是对的。输出顺序与我的预期不同。
这是主叫代码。
internal class Program
{
private static void Main(string[] args)
{
var levelOne = new LevelOne();
levelOne.Work();
Console.WriteLine("Waiting for answer...");
Console.ReadLine();
}
}
这是LevelOne类
public class LevelOne
{
public async void Work()
{
Console.WriteLine("Starting work.");
await LevelTwo.SomeRandomWork();
Console.WriteLine("Work done.");
}
}
最后这是LevelTwo类
public class LevelTwo
{
public static async Task SomeRandomWork()
{
Thread.Sleep(5000);
Console.WriteLine("Here I am");
var someRandomNumber = await KillTime();
Console.WriteLine("Answer " + someRandomNumber);
someRandomNumber = await KillTime();
Console.WriteLine("Answer again: " + someRandomNumber);
}
private static async Task<long> KillTime()
{
await Task.Delay(TimeSpan.FromMilliseconds(5000));
Random random = new Random();
return random.Next(100000, 900000);
}
}
我看到的是
Starting work.
Here I am
Waiting for answer...
Answer 352100
Answer again: 453356
Work done.
这是我期望的输出。
Starting work.
Waiting for answer...
Here I am
Answer 141710
Answer again: 530010
Work done.
在LevelOne的工作方法中等待后,控件是否应该返回到调用者代码?在LevelTwo被击中之前,控件不会再回来。
答案 0 :(得分:1)
这是因为您在Thread.Sleep
内使用的是await Task.Delay(...)
而不是SomeRandomWork()
。这就是为什么在await
SomeRandomWork()
内await
产生控件之前,Work()
内public static async Task SomeRandomWork() {
await Task.Delay(1000);
Console.WriteLine("Here I am");
var someRandomNumber = await KillTime();
Console.WriteLine("Answer " + someRandomNumber);
someRandomNumber = await KillTime();
Console.WriteLine("Answer again: " + someRandomNumber);
}
内的第一个Starting work.
Waiting for answer...
Here I am
Answer 598694
Answer again: 353771
Work done.
代码同步执行的原因:
{
"name": "angular-quickstart",
"version": "1.0.0",
"description": "QuickStart package.json from the documentation, supplemented with testing support",
"scripts": {
"build": "tsc -p src/",
"build:watch": "tsc -p src/ -w",
"build:e2e": "tsc -p e2e/",
"serve": "lite-server -c=bs-config.json",
"serve:e2e": "lite-server -c=bs-config.e2e.json",
"prestart": "npm run build",
"start": "concurrently \"npm run build:watch\" \"npm run serve\"",
"pree2e": "npm run build:e2e",
"e2e": "concurrently \"npm run serve:e2e\" \"npm run protractor\" --kill-others --success first",
"preprotractor": "webdriver-manager update",
"protractor": "protractor protractor.config.js",
"pretest": "npm run build",
"test": "concurrently \"npm run build:watch\" \"karma start karma.conf.js\"",
"pretest:once": "npm run build",
"test:once": "karma start karma.conf.js --single-run",
"lint": "tslint ./src/**/*.ts -t verbose"
},
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"@angular/common": "~4.1.3",
"@angular/compiler": "~4.1.3",
"@angular/core": "~4.1.3",
"@angular/forms": "~4.1.3",
"@angular/http": "~4.1.3",
"@angular/platform-browser": "~4.1.3",
"@angular/platform-browser-dynamic": "~4.1.3",
"@angular/router": "~4.1.3",
"@types/chart.js": "^2.7.2",
"angular-2-data-table": "^0.1.2",
"angular-in-memory-web-api": "~0.3.0",
"angular2-adal": "^0.1.4",
"angular2-csv": "^0.2.5",
"chart.js": "^2.7.1",
"core-js": "^2.4.1",
"jquery": "^3.2.1",
"ng-editable-table": "^0.3.15",
"ng2-accordion": "0.0.15",
"ng2-adal": "^0.2.6",
"ng2-bs3-modal": "^0.10.4",
"ng2-charts": "^1.6.0",
"ng2-smart-table": "^1.2.2",
"rxjs": "5.0.1",
"systemjs": "0.20.13",
"zone.js": "^0.8.4"
},
"devDependencies": {
"@types/jasmine": "2.5.51",
"@types/node": "^7.0.28",
"angular2-datatable": "^0.6.0",
"angular2-modal": "^2.0.3",
"ng2-bs3-modal": "0.10.4",
"ng2-accordion": "0.0.15",
"bootstrap-daterangepicker": "^2.1.25",
"canonical-path": "0.0.2",
"concurrently": "^3.2.0",
"daterange": "^0.3.0",
"daterangepicker": "^2.1.25",
"jasmine-core": "~2.6.2",
"jquery": "^3.2.1",
"karma": "^1.3.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-jasmine-html-reporter": "^0.2.2",
"lite-server": "^2.2.2",
"lodash": "^4.17.4",
"protractor": "~5.1.2",
"rimraf": "^2.5.4",
"tslint": "^5.4.3",
"typescript": "~2.3.4",
"typings": "^2.1.1"
},
"repository": {}
}
进行切换后,输出会更改为您的预期:
/**
* System configuration for Angular samples
* Adjust as necessary for your application needs.
*/
(function (global) {
System.config({
paths: {
// paths serve as alias
'npm:': '/node_modules/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
'app': 'app',
'directive': 'directives',
// angular bundles
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
'ng2-bs3-modal': 'npm:ng2-bs3-modal',
"ng2-accordion": "../../node_modules/ng2-accordion",
'angular-2-data-table': 'npm:angular-2-data-table/dist',
'ng2-charts': 'npm:ng2-charts/bundles/ng2-charts.umd.js',
'chart.js': 'npm:chart.js/dist/Chart.bundle.js',
'ng2-bs3-modal': 'npm:ng2-bs3-modal',
"ng2-accordion": "../../node_modules/ng2-accordion",
// other libraries
'rxjs': 'npm:rxjs',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
'angular2-datatable': 'npm:angular2-datatable',
'lodash': 'npm:lodash/lodash.js',
'ng2-adal': 'npm:ng2-adal',
'adal': 'npm:adal-angular/lib',
'adal-angular': 'npm:adal-angular/lib',
'angular2-csv': 'npm:angular2-csv',
'ng2-smart-table': 'npm:ng2-smart-table',
'ng2-completer': 'npm:ng2-completer',
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
'chart.js': {defaultExtension: 'js' },
'ng2-charts': { defaultExtension: 'js' },
'jquery': { main: 'jquery.js', defaultExtension: 'js' },
'ng2-smart-table': { main: 'bundles/table.umd.js', defaultExtension: 'js' },
'ng2-completer': { main: 'ng2-completer.umd.js', defaultExtension: 'js' },
'angular-2-data-table': {
format: 'cjs',
defaultExtension: 'js',
main: 'index.js'
},
'angular2-datatable': {
main: 'index.js',
defaultExtension: 'js'
},
'lodash': { defaultExtension: 'js' },
'ng2-accordion': { main: 'index.js', defaultExtension: "js" },
'ng2-bs3-modal': { main: 'ng2-bs3-modal.js', defaultExtension: "js" },
'directive': { defaultExtension: 'js' },
'ng2-accordion': { main: 'index.js', defaultExtension: "js" },
'ng2-bs3-modal': { main: 'ng2-bs3-modal.js', defaultExtension: "js" },
'ng2-adal': { main: 'core.js', defaultExtension: 'js' },
'adal-angular': { main: 'adal-angular', defaultExtension: 'js' },
'adal': { main: 'adal.js', defaultExtension: 'js' },
'angular2-csv': { main: 'Angular2-csv', defaultExtension: 'js' },
//'ng2-daterangepicker': {
// main: 'index',
// defaultExtension: 'js'
//},
//moment: {
// main: 'moment',
// defaultExtension: 'js'
//},
app: {
defaultExtension: 'js',
meta: {
'./*.js': {
loader: 'systemjs-angular-loader.js'
}
}
},
'rxjs': {
main: 'Rx.js',
defaultExtension: 'js'
}
}
});
})(this);
答案 1 :(得分:0)
虽然没有解决您的实际问题,但这可能对某人有用
await
关键字使运行时对新操作运行操作task
并使执行线程返回并继续执行 执行。 (在大多数情况下,它执行的主线程 应用)。 等待操作完成后,返回到 它停止的声明(换句话说,它返回给调用者, 换句话说,它根据保存的状态返回并开始 执行报表。
所以await
等待新任务完成,并确保等待任务执行完成后继续。
await
关键字与async
一起用于执行异步编程。它被称为异步编程,因为运行时在遇到await
关键字时(类似于迭代器中的yield
)捕获程序的状态,并且恢复在等待的任务完成后返回状态,以便继续在正确的上下文中运行。
它在某些方面类似于Task ContinueWith
然而,ContinueWith
和await
之间的一些显着差异是
保存状态并返回执行上下文
ContinueWith
不保存任何类型的状态,如果未提供调度程序,则使用默认线程调度程序上的ContinueWith
运行附加延续操作。
await
遇到此关键字时,状态会被保存,一旦完成task
的{{1}}完成,其执行流程将获取已保存的状态数据并启动await
之后的执行语句。 (注意:州正在详细说明await
/ executioncontext
。)。