Solved

How to control a thread

Posted on 2011-02-23
6
366 Views
Last Modified: 2013-12-17
Hello,

  I'm trying to create a thread in a ASP.NET site, where the user can start the thread and it keeps working up to its end even if the user leaves the site. The code I write is:

   GenerateOLAPData god = new GenerateOLAPData(_description, _fromDate, _toDate);
   Thread thread = new Thread(god.OLAPGenerator);
   thread.Start();

   GeraneteOLAPData connects to a database, setup the parameters and runs a long thread, populating a table with many records.

  It starts to work but sometime later the thread stops. Is there a timeout for it? How can I keep it working even if the user leaves the site?

  Thanks,

  Marco Castro
0
Comment
Question by:MarcoCastro
6 Comments
 

Author Comment

by:MarcoCastro
ID: 34966949
In a experience, if I go to the site during the thread process it stops faster. If I don't get in the site the thread keeps working!
0
 
LVL 51

Assisted Solution

by:Ted Bouskill
Ted Bouskill earned 166 total points
ID: 34971726
This isn't a good design.  When the page cycle ends and the last bytes are sent to the user all foreground threads are destroyed.  You can launch a background thread but it's orphaned and could be recycled by the application pool at any time based on the load of the server.

If you are using MS SQL fire up a SQL Agent job to execute the work from your code.
0
 
LVL 11

Accepted Solution

by:
Sudhakar Pulivarthi earned 167 total points
ID: 34978832
Hi,
I thread gets terminated when the code/method assign to it is completed. or The process which launched the thread is closed which will terminate all the threads assosiacted for the process. No timeouts

For your requirement. Its better to run a exe instead of a thread with in a process. When u launch a process it will run with out depending on the parent process launched it.

Check out this conversation:
http://www.velocityreviews.com/forums/t99961-thread-timeout.html
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 8

Assisted Solution

by:Volox
Volox earned 167 total points
ID: 35040124
I agree that this isn't the best design, but if you want the thread to keep working after the page and or session have completed, you will need to store the thread in a static variable.  And if you have the need for more than one thread (different users start their own thread) then you will need to use a static threadsafe collection that you key by some identifier for the user.
0
 

Author Closing Comment

by:MarcoCastro
ID: 35341901
The answers was so superficial
0
 
LVL 8

Expert Comment

by:Volox
ID: 35343557
MarcoCastro,

How is giving you advice that your thread variable needs to be a static varaible 'superficial' advice?  And if you needed an example of having one thread per user, you should have posted back and asked for it.  I don't think it is appropriate to give people's answers poor ratings if you aren't willing to post asking for clarifications or further information.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question