如何启动异步进程?

发布于 2024-07-28 22:22:22 字数 129 浏览 2 评论 0原文

我有一个网络表单,一旦填写完毕,就需要启动一个漫长的过程。 由于我不希望用户坐在那里等待它完成,因此我想启动任务,然后将用户带到另一个页面继续工作。

这样做是否涉及使用异步进程,如果是这样,有人有如何执行此操作的示例吗?

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

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

发布评论

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

评论(4

她如夕阳 2024-08-04 22:22:22

基本上,您需要一个“即发即忘”的实现,您可以在其中直接触发异步操作,而无需等待它完成。

查看以下相关问题的答案

代码是C#.Net,但vb.net应该类似。

编辑: Vb.net 解决方案:

这是异步触发 Web 方法的 vb.net 解决方案:

按如下方式考虑您的 Web 方法:

Public Class Service1
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Sub LengthyProcess()
        'Perform the lengthy operation
        'Also note this doesn't return anything
        'Hence can be used safely in fire-n-forget way of asynchronous delegate
        System.Threading.Thread.Sleep(1000)
        System.IO.File.WriteAllText("C:\\MyFile.txt", "This is the content to write!", Encoding.UTF8)
        System.Threading.Thread.Sleep(1000)
    End Sub

End Class

现在要异步调用 Web 方法,您可以执行以下任一操作,具体取决于您的.Net版本:

.Net 3.5(我不确定它是否适用于2.0)

设置Async =“true”

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._Default" Async="true" %>

确保在您的页面指令和后面的代码中

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Dim webService As MyService.Service1

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        webService = New MyService.Service1()
        webService.LengthyProcessAsync()
    End Sub

End Class

。 Net 2.0/ 1.0

无需设置 Async="true"

并且在代码隐藏中:

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Public Delegate Sub MyDelegateCallBack()
    Dim webService As MyService.Service1

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        webService = New MyService.Service1()
        Dim del As MyDelegateCallBack
        del = New MyDelegateCallBack(AddressOf webService.LengthyProcess)
        del.BeginInvoke(Nothing, Nothing)
    End Sub
End Class

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:

Public Class Service1
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Sub LengthyProcess()
        'Perform the lengthy operation
        'Also note this doesn't return anything
        'Hence can be used safely in fire-n-forget way of asynchronous delegate
        System.Threading.Thread.Sleep(1000)
        System.IO.File.WriteAllText("C:\\MyFile.txt", "This is the content to write!", Encoding.UTF8)
        System.Threading.Thread.Sleep(1000)
    End Sub

End Class

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

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._Default" Async="true" %>

And in the code behind:

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Dim webService As MyService.Service1

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        webService = New MyService.Service1()
        webService.LengthyProcessAsync()
    End Sub

End Class

.Net 2.0/ 1.0

No need to set Async="true"

And in the code-behind:

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Public Delegate Sub MyDelegateCallBack()
    Dim webService As MyService.Service1

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
        webService = New MyService.Service1()
        Dim del As MyDelegateCallBack
        del = New MyDelegateCallBack(AddressOf webService.LengthyProcess)
        del.BeginInvoke(Nothing, Nothing)
    End Sub
End Class
少钕鈤記 2024-08-04 22:22:22

这取决于过程。 它需要有多可靠? 如果进程崩溃了还可以吗? 崩溃后还需要恢复吗? 系统崩溃后?

如果您需要一定的可靠性,请在 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.

咽泪装欢 2024-08-04 22:22:22

查看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.

百善笑为先 2024-08-04 22:22:22

我发现针对“繁重”后台进程的最快且轻量级的解决方案是在控制台应用程序中编写逻辑,并使用 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.

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