在 ASP.NET 异步页面中,是否可以执行 2 个连续的异步任务?

发布于 2024-08-09 19:55:07 字数 408 浏览 3 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情魔剑神 2024-08-16 19:55:07

最简单的解决方案是在“EndAsyncOperation1”处理程序中启动任务2。

The easiest solution would be to launch task2 within the "EndAsyncOperation1" handler.

水溶 2024-08-16 19:55:07

如果任务 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.

眼泪也成诗 2024-08-16 19:55:07

解决方案是在 PageAsyncTask 构造函数中。将“并行”的布尔标志设置为 false,它们将按顺序运行。

The solution is in the PageAsyncTask constructor. Set the boolean flag for "parallel" to be false and they will run sequentially.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文