Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET Threading
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 28-Oct-10 2:45am
Rate this: bad
good
Please Sign up or sign in to vote.

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.
  Permalink  
Comments
Sandeep Mewara at 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 at 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 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,614
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 28 Oct 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100