Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a function which performs a series of time-consuming operations which include querying a large database, customising an excel file and sending an email with a 5MB attachment.
I would like to excute this function in the background, when a button is clicked, and immediately redirect the user to another aspx page. The user should be free to browse to other pages or even close the browser when the background operation is still running on the server.
I have tried to implement threading but could not get it to work. The email with attachment does not get sent even though there are no errors.

The code I have used is :
protected void Button1_Click(object sender, EventArgs e)
    {
        string OrderNo = obj_CustomerInfo.OrderNo;
        string CustomerFilePath = XLFile;
        object[] parameters = new object[] { OrderNo,CustomerFilePath };
        Thread FDthread = new Thread(new ParameterizedThreadStart(ProcessingOps));
        FDthread.IsBackground = true;
        FDthread.Start(parameters);
        Response.Redirect("NewPage.aspx");
    }
private static void ProcessingOps(object parameters)
    {
         object[] parameterArray = (object[])parameters;
         string OrderNo = (string)parameterArray[0];
         string CustomerFilePath = (string)parameterArray[1];
         // Time consuming operations....
    }
I would be very gratefull for any help or guidance as to how to achieve this.


Thanks and Regards
Meera
Posted

Threads don't work as you would expect under asp.net.
What you are better off doing is placing the work to be done in a database table as a queue and having another program such as a windows service go through that queue and do the processing.
What you return to the user is a message that their request has been added to the queue for processing, you may also want to have another page where the user can check the progress of the processing queue to see when their request has been processed but this is optional depending on requirements.
 
Share this answer
 
Comments
Sandeep Mewara 28-Oct-10 12:46pm    
Comment from OP:
Hi Rod,

Thanks for the reply.
The application am working on is hosted in a shared server. Would it be possible for me to implement a Windows Service in a such a case? Would it require any software installments on the server or specific permissions to get this to work?

Regards
Meera
Rod Kemp 28-Oct-10 20:36pm    
In the case of a shared server it is up to the people that manage the server so you would need to talk to them.
As for what is required have a look at BugTracker.Net http://sourceforge.net/projects/btnet/ the service this has receives email but you can change that to send email instead. The installation requirements are under in the documentation under "Receiving email using btnet_service.exe" http://www.ifdefined.com/doc_bug_tracker_pop3.html also look at the code in the btnet_service.cs file.
Rod Kemp 28-Oct-10 20:37pm    
If going down the road of a windows service doesn't work have a look at http://msdn.microsoft.com/en-us/library/system.web.ui.pageasynctask.aspx
kavya_m_82 29-Oct-10 2:21am    
Thank you once again. I shall verify is it is possible to use a windows service and also go through the link on how to use AsyncTasks
Regards
Meera

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900