Sorry if this is quite a long question, i need some advice on what people consider to be the best method (as there are a few).
At the moment, my project has a method
do_job(jobid,userid)
This method take a minute or two, and then redirects to another page.
This has been rewritten so that it now runs in the background and redirects to another page whilst the thread which do_job is on continues. – This works fine.
In a button_click event in a default.aspx page :
Dim d as New do_job_delegate(AddressOf do_job)
d.BeginInvoke(jobid,userid, New AsyncCallback(AddressOf Callback),d)
Response.redirect("results.aspx")
Which relates to
Delegate Sub do_job_delegate(Byval jobid as integer, Byval userid as integer)
Public Sub do_job(Byval jobid as integer, Byval userid as integer)
' Do something for several seconds
End Sub
Public Sub Callback(Byval ar As IAsyncResult)
ar.AsyncState.EndInvoke(ar)
End Sub
This basically does what i want, a user can carry on doing things on the site and the do_job method runs in the background and completes its tasks successfully.
However, i’ve now added to this do_job method, and there are several small tasks, each taking 30 or so seconds within do_job, and they could/should be made to run parallel.
e.g
Public Sub do_job(Byval jobid as integer, Byval userid as integer)
loop through between 1 or more (up to roughly 10 items)
'process that takes about 30 seconds
end loop
End Sub
So what i want to happen is – the longest time that do_job takes is the longest time of the longest job (30 seconds), rather than – 30 seconds x number of jobs = time it takes at the moment.
As i’m already using a delegate to detact the process from the ui, can anyone provide any advice/best practice on how i should rearrange my do_job method so that it can handle concurrent tasks to further boost the overall speed.
Thanks!
I would recommend that before you spend a bunch of time on this that you consider moving the work to a separate service rather than running it inline on your web server.
Just throw a record into a db table and have the service watch it.
Here is an article from Phil Haack explaining why you shouldn’t mess with threads on your web server.
http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx
As far as executing the tasks in parallel, look at the .net 4.0 Parallel libraries. They make this kind of thing super easy.
http://msdn.microsoft.com/en-us/library/dd460717.aspx