Multithreading in asp.net application

L

Leon

Is there a way I can create a thread at application level that running
all the time along with application exists?
I have tried to do the above thing, and I found for some reason, the
thread only can be excuted once after it is created.

Thanks very much for your help.
 
A

Alvin Bruney [MVP]

The life and breadth of a thread is axactly as long as it is permitted to
execute. Once the end of scope is reached, the thread is unscheduled and
cleaned up by the operating system. You will have to use some sort of sleep
inside the thread or loop to keep it constantly executing. A loop is not a
good idea because it is not an efficient wait state because it burns cpu
cycles. Use a sleep inside the body of the thread. Or better yet, use a
static timer started in the global asax file. Timers are implicitly threaded
anyway and there is no management involved.
 
D

Darren Clark

So are you saying that... i Can create a timer in the globa.asax and then i
can run things that wont effect my site? i mean,, this will run on its own
thread???

As i have a problem with our site in that i have to be able to send out
emails to users at certain time periods... So i could create a timer that
goes off in the global.asx and then it can then run my code? Otherwsie i
ened to wait for a users action to do it... and then i dont want the user to
wait till all the emails have been sent.




Alvin Bruney said:
The life and breadth of a thread is axactly as long as it is permitted to
execute. Once the end of scope is reached, the thread is unscheduled and
cleaned up by the operating system. You will have to use some sort of sleep
inside the thread or loop to keep it constantly executing. A loop is not a
good idea because it is not an efficient wait state because it burns cpu
cycles. Use a sleep inside the body of the thread. Or better yet, use a
static timer started in the global asax file. Timers are implicitly threaded
anyway and there is no management involved.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/27cok
Leon said:
Is there a way I can create a thread at application level that running
all the time along with application exists?
I have tried to do the above thing, and I found for some reason, the
thread only can be excuted once after it is created.

Thanks very much for your help.
 
L

Leon

I have put the Thread in a loop with putting it into sleep, didn't
work for me. But, to use a timer in the globa.asax file is a greet
idea, thanks a lot. Right now, I have no ideas how to create a timer
in the globa.asax file, and wheather it can be created programmly or
not, so, could you give me some hits on those as well.

Thanks again.





Darren Clark said:
So are you saying that... i Can create a timer in the globa.asax and then i
can run things that wont effect my site? i mean,, this will run on its own
thread???

As i have a problem with our site in that i have to be able to send out
emails to users at certain time periods... So i could create a timer that
goes off in the global.asx and then it can then run my code? Otherwsie i
ened to wait for a users action to do it... and then i dont want the user to
wait till all the emails have been sent.




Alvin Bruney said:
The life and breadth of a thread is axactly as long as it is permitted to
execute. Once the end of scope is reached, the thread is unscheduled and
cleaned up by the operating system. You will have to use some sort of sleep
inside the thread or loop to keep it constantly executing. A loop is not a
good idea because it is not an efficient wait state because it burns cpu
cycles. Use a sleep inside the body of the thread. Or better yet, use a
static timer started in the global asax file. Timers are implicitly threaded
anyway and there is no management involved.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/27cok
Leon said:
Is there a way I can create a thread at application level that running
all the time along with application exists?
I have tried to do the above thing, and I found for some reason, the
thread only can be excuted once after it is created.

Thanks very much for your help.
 
J

John Saunders

Leon said:
Is there a way I can create a thread at application level that running
all the time along with application exists?
I have tried to do the above thing, and I found for some reason, the
thread only can be excuted once after it is created.


The use of threads in an ASP.NET application is a very advanced technique
and should be avoided if at all possible.

In a later reply, you mention periodically sending e-mails. I recommend that
you do this in a separate application, not as part of your web application.
I frequently write small console applications to do e-mails based on
information newly added to the database. I have these run as Scheduled Tasks
every 15 minutes or so.
 
A

Alvin Bruney [MVP]

So are you saying that... i Can create a timer in the globa.asax and then
i
can run things that wont effect my site? i mean,, this will run on its own
thread???
yup. It will affect your site. It's running in the application object which
has global scope so it is not a second class citizen. you have to treat it
like a first class citizen.
As i have a problem with our site in that i have to be able to send out
emails to users at certain time periods... So i could create a timer that
goes off in the global.asx and then it can then run my code?
Yup. So for example, set the timer to go off every 2 days. When that time
period passes, the timer fires and calls the event handler. Your code in the
event handler runs, spams 10000 people for example. When the event handler
has terminated, the timer sleeps. Well actually, the thread on which the
timer event is firing enters an efficient wait state for the next two days.

Here is the code i use. It's customized a bit for my own purposes because i
wanted access to the timer from any page in my application at any time so
that i could interrupt the timer when i felt like it.

Drag a timer onto the global.asax form. Set your timer interval. It's in
milliseconds.
Change the definition to include a static reference.
private System.Timers.Timer timer1 to
public static System.Timers.Timer timer1; //notice i changed private to
public because i need access to it from other files.
Click on the timer control on the global.asax file and add an event handler
for the Elapsed event. It creates it's own handler. Place your code inside
this handler.

Compile. It will fail with errors inside the initializecomponent routine for
the global.asax.cs file. This is because, a static instance cannot have a
this reference. So remove the this. references for the timer inside the
initializecomponent for global.asax.cs file for every place the compiler
complains. this.Timer1 = new System.Timers.Timer(); to Timer1 = new
System.Timers.Timer(); for example. Your initializecomponent global should
look like this

private void InitializeComponent()
{
timer1 = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)(timer1)).BeginInit();
//
// timer1
//
timer1.Enabled = true;
timer1.Elapsed += new
System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
((System.ComponentModel.ISupportInitialize)(timer1)).EndInit();

}
Recompile and your are good to go.

The reason I like to create the static instance of the timer is that in
another page, say webform1.aspx i want to be able to control the timer. For
example, in webform1.aspx page_load routine, i can do this

Test.Global.timer1.Stop();

where test is my namespace and global is the class where the timer is
declared

There's a pesky bug with studio. If you touch the timer again on the design
form. It removes the static reference. So look out for this.

Pay special attention to the code inside the timer event handler. Make sure
objects are cleaned up and there is exception handling in there. A timer
going south can leak resources every time it fires eventually crashing the
server.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/27cok
Darren Clark said:
So are you saying that... i Can create a timer in the globa.asax and then
i
can run things that wont effect my site? i mean,, this will run on its own
thread???

As i have a problem with our site in that i have to be able to send out
emails to users at certain time periods... So i could create a timer that
goes off in the global.asx and then it can then run my code? Otherwsie i
ened to wait for a users action to do it... and then i dont want the user
to
wait till all the emails have been sent.




Alvin Bruney said:
The life and breadth of a thread is axactly as long as it is permitted to
execute. Once the end of scope is reached, the thread is unscheduled and
cleaned up by the operating system. You will have to use some sort of sleep
inside the thread or loop to keep it constantly executing. A loop is not
a
good idea because it is not an efficient wait state because it burns cpu
cycles. Use a sleep inside the body of the thread. Or better yet, use a
static timer started in the global asax file. Timers are implicitly threaded
anyway and there is no management involved.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/27cok
Leon said:
Is there a way I can create a thread at application level that running
all the time along with application exists?
I have tried to do the above thing, and I found for some reason, the
thread only can be excuted once after it is created.

Thanks very much for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top