473,386 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 2035
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
by: mihai | last post by:
What does the standard say about those two? Is any assurance that the use of STL is thread safe? Have a nice day, Mihai.
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
9
by: Popoxinhxan | last post by:
Dear experts, i want to develop an client application that consume the google search web service. In my MainForm i have a method to retrieve all the search result e.g. GetGoogleResults(). Now i...
9
by: tommy | last post by:
hi, i have found a example for multithreading and asp.net http://www.fawcette.com/vsm/2002_11/magazine/features/chester/ i want to speed up my website ... if my website is starting, they...
2
by: Rich | last post by:
Hello, I have set up a multithreading routine in a Test VB.net proj, and it appears to be working OK in debug mode and I am not using synchronization. Multithreading is a new thing for me, and...
55
by: Sam | last post by:
Hi, I have a serious issue using multithreading. A sample application showing my issue can be downloaded here: http://graphicsxp.free.fr/WindowsApplication11.zip The problem is that I need to...
4
by: boo73uk | last post by:
Hi All, I'm going to rewrite a VB6 app to VB.net and I need some pointers. Basically this app spawns simultaneous,multiple, independant ActiveX.exe 'workers' which query a SQL Server database and...
4
by: Michael | last post by:
Hi, I am trying to create a multithreaded VB 2005 application which attempts to create a new thread per Domain Controller (DC) in my environment. Each thread connects to its allocated DC and...
2
by: Pradnya Patil | last post by:
hi , I am trying to draw ' html div-tag ' on the screen which will resemble a rectangle through vb.net code. I want it to be drawn faster...so I introduced multithreading using Threadpool. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.