Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to control a thread

Posted on 2011-02-23
6
Medium Priority
?
375 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 332 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 334 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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 8

Assisted Solution

by:Volox
Volox earned 334 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

877 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