471,092 Members | 1,148 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,092 software developers and data experts.

Multithreading in asp.net application

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.
Nov 18 '05 #1
5 1915
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" <pe*******@hotmail.com> wrote in message
news:89**************************@posting.google.c om...
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.

Nov 18 '05 #2
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 [MVP]" <vapor at steaming post office> wrote in message
news:eh*************@TK2MSFTNGP12.phx.gbl...
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" <pe*******@hotmail.com> wrote in message
news:89**************************@posting.google.c om...
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.


Nov 18 '05 #3
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" <dc******@hotmail.com> wrote in message news:<0H****************@news-server.bigpond.net.au>...
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 [MVP]" <vapor at steaming post office> wrote in message
news:eh*************@TK2MSFTNGP12.phx.gbl...
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" <pe*******@hotmail.com> wrote in message
news:89**************************@posting.google.c om...
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.


Nov 18 '05 #4
> 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_Elap sed);
((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" <dc******@hotmail.com> wrote in message
news:0H****************@news-server.bigpond.net.au... 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 [MVP]" <vapor at steaming post office> wrote in message
news:eh*************@TK2MSFTNGP12.phx.gbl...
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" <pe*******@hotmail.com> wrote in message
news:89**************************@posting.google.c om...
> 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.



Nov 18 '05 #5
"Leon" <pe*******@hotmail.com> wrote in message
news:89**************************@posting.google.c om...
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.
--
John Saunders
John.Saunders at SurfControl.com
Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

47 posts views Thread by mihai | last post: by
16 posts views Thread by Robert Zurer | last post: by
9 posts views Thread by Popoxinhxan | last post: by
9 posts views Thread by tommy | last post: by
2 posts views Thread by Rich | last post: by
55 posts views Thread by Sam | last post: by
4 posts views Thread by boo73uk | last post: by
4 posts views Thread by Michael | last post: by
2 posts views Thread by Pradnya Patil | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.