System.Timers.Timer

Discussion in 'ASP .Net' started by Rob, Sep 26, 2007.

  1. Rob

    Rob Guest

    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.
     
    Rob, Sep 26, 2007
    #1
    1. Advertising

  2. Rob

    bruce barker Guest

    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)



    Rob wrote:
    > 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.
    >
    >
     
    bruce barker, Sep 26, 2007
    #2
    1. Advertising

  3. Rob

    Rob Guest

    > 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.
     
    Rob, Sep 29, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kelsang Wangchuk

    System.Timers.Timer vs. System.Threading.Timer

    Kelsang Wangchuk, Jul 31, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    761
    Kelsang Wangchuk
    Jul 31, 2003
  2. Mark Rae

    System.Timers.Timer in Global.asax

    Mark Rae, Feb 3, 2005, in forum: ASP .Net
    Replies:
    10
    Views:
    10,531
    ehosca
    May 31, 2009
  3. Replies:
    1
    Views:
    1,687
    Steve C. Orr [MVP, MCSD]
    Feb 22, 2005
  4. Shawn B.
    Replies:
    2
    Views:
    557
    Scott Allen
    Apr 28, 2005
  5. Dave Hagerich

    newbie quest about System.Timers.Timer

    Dave Hagerich, Dec 12, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    766
    S. Justin Gengo
    Dec 12, 2005
Loading...

Share This Page