如何从 ASP.NET 页面运行冗长的任务?

发布于 2024-10-17 19:20:09 字数 571 浏览 0 评论 0原文

我有一个带有简单表单的 ASP.NET 页面。用户填写表单并提供一些详细信息,上传文档,然后需要在服务器端对文件进行一些处理。

我的问题是 - 处理文件的服务器端处理的最佳方法是什么?该处理涉及调用 exe。我应该为此使用单独的线程吗?

理想情况下,我希望用户提交表单,而在处理过程中网页不会挂在那里。

我已经尝试过这段代码,但我的任务从未在服务器上运行:

Action<object> action = (object obj) =>
{
      // Create a .xdu file for this job
       string xduFile = launcher.CreateSingleJobBatchFile(LanguagePair, SourceFileLocation);

      // Launch the job                 
      launcher.ProcessJob(xduFile);
};

Task job = new Task(action, "test");
job.Start();

任何建议都表示赞赏。

I've got an ASP.NET page with a simple form. The user fills out the form with some details, uploads a document, and some processing of the file then needs to happens on the server side.

My question is - what's the best approach to handling the server side processing of the files? The processing involves calling an exe. Should I use seperate threads for this?

Ideally I want the user to submit the form without the web page just hanging there while the processing takes place.

I've tried this code but my task never runs on the server:

Action<object> action = (object obj) =>
{
      // Create a .xdu file for this job
       string xduFile = launcher.CreateSingleJobBatchFile(LanguagePair, SourceFileLocation);

      // Launch the job                 
      launcher.ProcessJob(xduFile);
};

Task job = new Task(action, "test");
job.Start();

Any suggestions are appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

半步萧音过轻尘 2024-10-24 19:20:09

您可以以经典的即发即忘方式异步调用处理功能:

在 .NET 4.0 中,您应该使用新的 任务并行库

Task.Factory.StartNew(() =>
{
    // Do work
});

如果您需要将参数传递给操作委托,您可以这样做:

Action<object> task = args =>
{
    // Do work with args
};    
Task.Factory.StartNew(task, "SomeArgument");

在 .NET 3.5 及更早版本中,您可以这样做:

ThreadPool.QueueUserWorkItem(args =>
{
   // Do work
});

相关资源:

You could invoke the processing functionality asynchronously in a classic fire and forget fashion:

In .NET 4.0 you should do this using the new Task Parallel Library:

Task.Factory.StartNew(() =>
{
    // Do work
});

If you need to pass an argument to the action delegate you could do it like this:

Action<object> task = args =>
{
    // Do work with args
};    
Task.Factory.StartNew(task, "SomeArgument");

In .NET 3.5 and earlier you would instead do it this way:

ThreadPool.QueueUserWorkItem(args =>
{
   // Do work
});

Related resources:

野鹿林 2024-10-24 19:20:09

用途:

ThreadPool.QueueUserWorkItem(o => MyFunc(arg0, arg1, ...));

其中MyFunc()在用户提交页面后在后台进行服务器端处理;

Use:

ThreadPool.QueueUserWorkItem(o => MyFunc(arg0, arg1, ...));

Where MyFunc() does the server-side processing in the background after the user submits the page;

甜扑 2024-10-24 19:20:09

我有一个网站,它执行一些可能需要长时间运行的操作,这些操作需要响应并为用户更新计时器。

我的解决方案是在页面中构建一个带有隐藏值和一些会话值的状态机。

我的 aspx 端有这些:

<asp:Timer ID="Timer1" runat="server" Interval="1600" />
<asp:HiddenField runat="server" ID="hdnASynchStatus" Value="" />

我的代码看起来像这样:

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

Private Sub PostbackStateEngineStep()
    If hdnASynchStatus.Value = "" And Not CBool(Session("WaitingForCallback")) Then

        Dim i As IAsyncResult = {...run something that spawns in it's own thread, and calls ProcessCallBack when it's done...}

        Session.Add("WaitingForCallback", True)
        Session.Add("InvokeTime", DateTime.Now)
        hdnASynchStatus.Value = "WaitingForCallback"
    ElseIf CBool(Session("WaitingForCallback")) Then
        If Not CBool(Session("ProcessComplete")) Then
            hdnASynchStatus.Value = "WaitingForCallback"
        Else
            'ALL DONE HERE
            'redirect to the next page now
            response.redirect(...)
        End If
    Else
        hdnASynchStatus.Value = "DoProcessing"
    End If
End Sub
Public Sub ProcessCallBack(ByVal ar As IAsyncResult)
    Session.Add("ProcessComplete", True)
End Sub
Private Sub UpdateElapsedTime()
    'update a label with the elapsed time
End Sub

I have a site that does some potentially long running stuff that needs to be responsive and update a timer for the user.

My solution was to build a state machine into the page with a hidden value and some session values.

I have these on my aspx side:

<asp:Timer ID="Timer1" runat="server" Interval="1600" />
<asp:HiddenField runat="server" ID="hdnASynchStatus" Value="" />

And my code looks something like:

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

Private Sub PostbackStateEngineStep()
    If hdnASynchStatus.Value = "" And Not CBool(Session("WaitingForCallback")) Then

        Dim i As IAsyncResult = {...run something that spawns in it's own thread, and calls ProcessCallBack when it's done...}

        Session.Add("WaitingForCallback", True)
        Session.Add("InvokeTime", DateTime.Now)
        hdnASynchStatus.Value = "WaitingForCallback"
    ElseIf CBool(Session("WaitingForCallback")) Then
        If Not CBool(Session("ProcessComplete")) Then
            hdnASynchStatus.Value = "WaitingForCallback"
        Else
            'ALL DONE HERE
            'redirect to the next page now
            response.redirect(...)
        End If
    Else
        hdnASynchStatus.Value = "DoProcessing"
    End If
End Sub
Public Sub ProcessCallBack(ByVal ar As IAsyncResult)
    Session.Add("ProcessComplete", True)
End Sub
Private Sub UpdateElapsedTime()
    'update a label with the elapsed time
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文