在 ASP.NET 异步页面中,是否可以执行 2 个连续的异步任务?
在 ASP.NET 中,您可以按如下方式运行异步任务:
PageAsyncTask task1 =
new PageAsyncTask(BeginAsyncOperation1, EndAsyncOperation1, TimeoutAsyncOperation1, state);
RegisterAsyncTask(task1);
PageAsyncTask task2 =
new PageAsyncTask(BeginAsyncOperation2, EndAsyncOperation2, TimeoutAsyncOperation2, state);
RegisterAsyncTask(task2);
但是,假设您需要确保任务 1 在任务 2 执行之前完成。这可能吗?
我的理解是这些任务将并行运行。
In ASP.NET, you can run asynchronous tasks as follows:
PageAsyncTask task1 =
new PageAsyncTask(BeginAsyncOperation1, EndAsyncOperation1, TimeoutAsyncOperation1, state);
RegisterAsyncTask(task1);
PageAsyncTask task2 =
new PageAsyncTask(BeginAsyncOperation2, EndAsyncOperation2, TimeoutAsyncOperation2, state);
RegisterAsyncTask(task2);
However, suppose you need to ensure that task1 completes before task2 executes. Is this possible?
My understanding is that these tasks would run in parallel.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的解决方案是在“EndAsyncOperation1”处理程序中启动任务2。
The easiest solution would be to launch task2 within the "EndAsyncOperation1" handler.
如果任务 1 需要在任务 2 之前完成,您可以考虑创建第三个操作,该操作封装对 BeginAsyncOperation1 和 BeginAsyncOperation2 的同步调用,并异步运行该操作。在第一个任务完成后,第二个任务的工作才会开始。
If task1 needs to complete before task2, you could consider creating a third operation that encapsulates synchronous calls to BeginAsyncOperation1 and BeginAsyncOperation2 and run that operation asynchronously. The work for the second task will not begin until the first task has completed.
解决方案是在 PageAsyncTask 构造函数中。将“并行”的布尔标志设置为 false,它们将按顺序运行。
The solution is in the PageAsyncTask constructor. Set the boolean flag for "parallel" to be false and they will run sequentially.