How to perform lengthy tasks from an ASP.NET page?

I have an ASP.NET page with a simple form. The user fills out the form with some details, uploads the document, and some file processing should then be done on the server side.

My question is: what is the best approach to server side file processing? Processing includes an exe call. Should I use separate threads for this?

Ideally, I want the user to submit the form without the web page just hanging there during processing.

I tried this code, but my task never ran 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 welcome.

+4
source share
3 answers

You can use the processing functions asynchronously in the classic style and forget :

In .NET 4.0, you must do this using the new parallel task library :

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

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

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

In .NET 3.5 and earlier, you could do it like this:

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

Related Resources:

+7
source

Using:

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

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

+2
source

I have a website that does some potentially long-running things that should respond and update the timer for the user.

My solution was to create a state machine on a page with a hidden value and some session values.

I have this on the aspx side:

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

And my code looks something like this:

 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 own thread, and calls ProcessCallBack when it 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 
+1
source

Source: https://habr.com/ru/post/1340320/


All Articles