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.

2 solutions

Solution 1

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.

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?

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.