Reputation: 1832
I have a web application and in the code behind page, I have a button click event.
protected void btnUpload_Click(object sender, EventArgs e)
{
TimerEvent TE = new TimerEvent();
TE.TimerEvents();
Server.Transfer("~/Jobs.aspx");
}
I am having a method call TimerEvents() and it may take 10-15 seconds to get the control back to the server transfer. so I want the server transfer line to be executed simultaneously at the same time of the method call.
I tried threading as in here:
TimerEvent TE = new TimerEvent();
Thread t = new Thread(TE.TimerEvents);
t.IsBackground = true;
t.Start();
Server.Transfer("~/Jobs.aspx");
But the transfer is not made, until the method is finished. How can I achieve this?
Upvotes: 5
Views: 1421
Reputation: 2524
easiest thing to do: i would just return immediately and say "You're file has been uploaded" but then do a auto refreshing on your 'jobs.aspx' page and show the status for each job as "Processing" until it's "Done"
assuming jobs.aspx is a list of your processing jobs... and that 'auto refresh' isn't too janky :)
Upvotes: 0
Reputation: 2942
If the process is repeated in a similar fashion often, what you would call an "application event" why not settup a process on a sperate thread from the Global.asax (application) file. You could use the FileWatcher class to monitor the directory and process the files sequentially or by launching another process in its own thread for each file as they arrive, you could persist a status and any results and expose this to the client possibly using an AJAX timed callback.
Remember to handle all exceptions properly in methods called from the Global.asax file as they can bring the whole appDomain down.
Upvotes: 0
Reputation: 1105
This design is not ideal. Even if you get it right, there's a chance you'll run into problems down the road due to the mixing of multi-threading code and the Server.Transfer() call.
There are also issues regarding multi-threading in ASP.NET applications.
I suggest you re-engineer your app so that you have some kind of queue that queues up long-running tasks. You can then then have a service that processes tasks in the queue one by one.
From your web application, you'll now be able to:
Queue up the long running TimerEvents task to the external service.
Call Server.Transfer()
On the new page ("~/Jobs.aspx"), call the external service to pick up your results when you need it.
Upvotes: 5