如何启动异步进程?
我有一个网络表单,一旦填写完毕,就需要启动一个漫长的过程。 由于我不希望用户坐在那里等待它完成,因此我想启动任务,然后将用户带到另一个页面继续工作。
这样做是否涉及使用异步进程,如果是这样,有人有如何执行此操作的示例吗?
I have a web form that once filled out needs to kick off a lengthy process. Since I don't want the user sitting there waiting for it to complete, I want to kick off the task and then take the user to another page to continue working.
Would doing this involve using an asynchronus process, and if so, does someone have an example of how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
基本上,您需要一个“即发即忘”的实现,您可以在其中直接触发异步操作,而无需等待它完成。
查看以下相关问题的答案。
代码是C#.Net,但vb.net应该类似。
编辑: Vb.net 解决方案:
这是异步触发 Web 方法的 vb.net 解决方案:
按如下方式考虑您的 Web 方法:
现在要异步调用 Web 方法,您可以执行以下任一操作,具体取决于您的.Net版本:
.Net 3.5(我不确定它是否适用于2.0)
设置Async =“true”
确保在您的页面指令和后面的代码中
: 。 Net 2.0/ 1.0
无需设置 Async="true"
并且在代码隐藏中:
Basically you need a fire-n-forget implementation where you can just fire an async operation without waiting for it to finish.
Check out the following answer on a related question.
The code is in C#.Net, but vb.net should be similar.
Edit: Vb.net Solution:
This is the vb.net solution to fire a web method asynchronously:
Consider your web method as follows:
Now to call the web method asynchronously you can do either of the following depending your version of .Net:
.Net 3.5 (I am not sure if it works in 2.0)
Make sure to set Async="true" in your Page directive
And in the code behind:
.Net 2.0/ 1.0
No need to set Async="true"
And in the code-behind:
这取决于过程。 它需要有多可靠? 如果进程崩溃了还可以吗? 崩溃后还需要恢复吗? 系统崩溃后?
如果您需要一定的可靠性,请在 Windows 服务中托管此长时间运行的任务。 通过使用 WCF 传递请求,在 Web 应用程序和 Windows 服务之间进行通信。 您甚至可以使用 MSMQ 连接来确保请求不会丢失,并且服务可以一次接收一个请求。
该服务可以配置为在 Windows 启动时启动,并在 Windows 崩溃时重新启动。
It depends on the process. How reliable does it need to be? Is it ok if the process crashes? Does it need to recover after it crashes? After the system crashes?
If you need some reliability, then host this long-running task in a Windows Service. Communicate between the web application and the Windows Service by using WCF to pass requests. You can even use an MSMQ connection to make sure the requests are not lost, and the service can pick them up one at a time.
The service can be configured to start when Windows starts, and to restart if it crashes.
查看BackgroundWorker组件:
BackgroundWorker 组件。 这里的示例展示了使用它来更新 VB 应用程序中的 UI,但原理是通用的。
Check out the BackgroundWorker component:
BackgroundWorker Component. The sample here shows using it to update the UI in a VB application, but the principle's universal.
我发现针对“繁重”后台进程的最快且轻量级的解决方案是在控制台应用程序中编写逻辑,并使用 Windows 调度程序定期运行该应用程序或在需要时触发它。
我发现这比编写 Windows 服务要好得多,因为它需要更少的错误处理(即,如果服务终止,则需要恢复,而如果控制台应用程序终止,只需发送错误通知并等待下一次运行。
对于任何大型我编码的网站我还做了这些控制台应用程序之一来处理所有“非用户生成”事件以及重型事件。
I have found the quickest and lightweight solution for "heavy" background processes is to code the logic in a Console app and use windows scheduler to run the app periodically or trigger it when required.
I have found this much better than coding a windows service as it requires much less error handling (ie if the service dies it needs to recover whereas if the console app dies, just send an error notification and wait until the next run.
For any large website I code I also do one of these console apps to handle all the "non user generated" events as well as the heavy ones.