473,786 Members | 2,578 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2067
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*******@hotm ail.com> wrote in message
news:89******** *************** ***@posting.goo gle.com...
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******** *****@TK2MSFTNG P12.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*******@hotm ail.com> wrote in message
news:89******** *************** ***@posting.goo gle.com...
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******@hotma il.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******** *****@TK2MSFTNG P12.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*******@hotm ail.com> wrote in message
news:89******** *************** ***@posting.goo gle.com...
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.T imer timer1 to
public static System.Timers.T imer 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 initializecompo nent 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
initializecompo nent for global.asax.cs file for every place the compiler
complains. this.Timer1 = new System.Timers.T imer(); to Timer1 = new
System.Timers.T imer(); for example. Your initializecompo nent global should
look like this

private void InitializeCompo nent()
{
timer1 = new System.Timers.T imer();
((System.Compon entModel.ISuppo rtInitialize)(t imer1)).BeginIn it();
//
// timer1
//
timer1.Enabled = true;
timer1.Elapsed += new
System.Timers.E lapsedEventHand ler(this.timer1 _Elapsed);
((System.Compon entModel.ISuppo rtInitialize)(t imer1)).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.tim er1.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******@hotma il.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******** *****@TK2MSFTNG P12.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*******@hotm ail.com> wrote in message
news:89******** *************** ***@posting.goo gle.com...
> 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*******@hotm ail.com> wrote in message
news:89******** *************** ***@posting.goo gle.com...
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
3757
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
4270
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 threads of a multithreaded program using embedded SQL. Since the threads do not need to share transaction scopes, the sqleAttachToCtx family of APIs do not seem to be necessary. <quote> In the default implementation of threaded applications against...
16
8510
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
13619
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 want to have a nice progress bar in another form e.g ProgressForm to perform the search action. i tried to do it but the progress bar won't work and the GUI freezed. I knew how to do with the single threading but i don't know how to do it in...
9
2465
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 should build a database-connection and send a few sqls
2
2312
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 I just wanted to ask if I am missing anything based on the following scenario. My test app pulls data from a large external data source which has a table-like structure (but not rdbms - more
55
3330
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 call operations on Controls from a delegate, otherwise it does not work. However each time I've done an operation, I must update the progressbar and progresslabel, but this cannot be done in the delegate as it does not work.
4
2672
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 BCP a load of data out. Now, what is the best way to approach this in .Net? Can .Net give me a better way of utilising the resources of the server? We're also getting some new hardware, 2 CPU Xeon rig with HT (hyper threading). Are there any...
4
1573
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 enumerates all computer objects and extracts the 'LastLogon' property. The results from each thread is then consolidated so that I can get the true lastlogon date for each computer object. However in my routine thats get actioned per thread, I have...
2
2267
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 divided the complete drawing into 3 parts..1st will be done by main thread and other two are done in these procedures - <1LongTimeTask <2LongTimeTask2 I have invoked the threads using below method. **************
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9496
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.