System.Timers.Timer

R

Rob

I've got the following skeleton in my HttpApplication (global.aspx) file:

Public Sub New()
MyBase.New()
ApplicationTimer = New System.Timers.Timer
ApplicationTimer.BeginInit()
ApplicationTimer.Interval = 60 * 1000 ' 60 seconds
ApplicationTimer.Enabled = True
ApplicationTimer.EndInit()
End Sub

Private Sub ApplicationTimer_Elapsed(ByVal sender As Object, ByVal e
As System.Timers.ElapsedEventArgs) Handles ApplicationTimer.Elapsed
' Do something time consuming
End Sub

Is ApplicationTimer_Elapsed run on a separate thread to those processing
HTTP requests? The reason I ask is that the code I'm about to drop into
ApplicationTimer_Elapsed takes several seconds to run. It's sending an email
via SmtpClient.Send(MailMessage) which doesn't seem to return until the
email has been sent.

I don't want this operation to slow up supplying pages to users of the
system requesting web pages.

Thanks, Rob.
 
B

bruce barker

when a request starts, a processing thread is picked from the pool.
the request thread first job is to get an application object from the
application object pool. if none are free it creates one. when the
request finishes, the application object and request threads are
returned to their pools.

your timer will alway be running on a request thread, and may block any
request. if the same request thread creates two application objects
(different request same thread), then the time will be runnning more
than once on the same thread. it can also be running on two threads.

any response.end aborts the current request thread and will stop any
timer you started on that thread.

you probably want to start one background thread to do this. use locks
and a static or application start to only start only one background thread.

-- bruce (sqlwork.com)
 
R

Rob

when a request starts, a processing thread is picked from the pool.

I don't think this is an HTTP request - it's the core webapp's application
object, inherited from HttpApplication. It's called global.aspx but I think
that's a hangover from ASP v1, it can be called anything in ASP v2.

So I'm assuming that I'm adding a timer to the main application thread so I
wouldn't expect the request thread pool to be involved. But I am guessing.

Having an event take several seconds to run doesn't appear to cause dealing
of requests to slow significantly, i.e. the processing of requests via the
thread pool carries on as normal.

Cheers, Rob.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top