Timer.Elapsed event doesn't want to fire

Discussion in 'ASP .Net Building Controls' started by Nathan Sokalski, Jul 15, 2005.

  1. I am trying to learn how to use the System.Timers.Timer control to perform
    an action every certain amount of time. However, the Elapsed event doesn't
    want to fire, but I can't figure out why. I looked at several code examples
    online, but I think I was doing everything the same way they were. Does
    anybody have a complete example in VB.NET (the whole aspx and aspx.vb file
    so I know I am including everything) that I can look at? Any help would be
    appreciated. Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
     
    Nathan Sokalski, Jul 15, 2005
    #1
    1. Advertising

  2. Nathan Sokalski

    Anand[MVP] Guest

    Are you trying to use this in a ASP.NET Page? Pages live only for the time of
    the request. Once a request is served, the Page class is destroyed.

    --
    Rgds,
    Anand
    VB.NET MVP
    http://www.dotnetindia.com


    "Nathan Sokalski" wrote:

    > I am trying to learn how to use the System.Timers.Timer control to perform
    > an action every certain amount of time. However, the Elapsed event doesn't
    > want to fire, but I can't figure out why. I looked at several code examples
    > online, but I think I was doing everything the same way they were. Does
    > anybody have a complete example in VB.NET (the whole aspx and aspx.vb file
    > so I know I am including everything) that I can look at? Any help would be
    > appreciated. Thanks.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    >
    >
     
    Anand[MVP], Jul 15, 2005
    #2
    1. Advertising

  3. Nathan Sokalski

    stand__sure Guest

    for asp.net, anand is correct.

    for all languages in general, timers are not guaranteed to fire on
    time. this happens for two reason: 1) task switching at the CPU; and
    2) WM_TIMER messages have a lower priority than other messages (like
    device input)
     
    stand__sure, Jul 15, 2005
    #3
  4. Nathan Sokalski

    Rothariger Guest

    hi, there are two types of timers, one is at windows forms, and the other is
    at components, one works just fine, and the other its just crap... i didnt
    know the difference, but so is it...


    salute!

    "Nathan Sokalski" wrote:

    > I am trying to learn how to use the System.Timers.Timer control to perform
    > an action every certain amount of time. However, the Elapsed event doesn't
    > want to fire, but I can't figure out why. I looked at several code examples
    > online, but I think I was doing everything the same way they were. Does
    > anybody have a complete example in VB.NET (the whole aspx and aspx.vb file
    > so I know I am including everything) that I can look at? Any help would be
    > appreciated. Thanks.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    >
    >
     
    Rothariger, Jul 15, 2005
    #4
  5. I understand that that is true for the actual pages, but is there a way to
    use a timer in the Global.asax.vb file? Doesn't that live the entire life of
    the application? My basic goal is to find a way to periodically send myself
    stats about what people do at my site, and send email newsletters. And there
    is obviously some purpose for the System.Timers.Timer in ASP.NET since
    Visual Studio lets you add it to an ASP.NET webform.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
    "Anand[MVP]" <> wrote in message
    news:...
    > Are you trying to use this in a ASP.NET Page? Pages live only for the time
    > of
    > the request. Once a request is served, the Page class is destroyed.
    >
    > --
    > Rgds,
    > Anand
    > VB.NET MVP
    > http://www.dotnetindia.com
    >
    >
    > "Nathan Sokalski" wrote:
    >
    >> I am trying to learn how to use the System.Timers.Timer control to
    >> perform
    >> an action every certain amount of time. However, the Elapsed event
    >> doesn't
    >> want to fire, but I can't figure out why. I looked at several code
    >> examples
    >> online, but I think I was doing everything the same way they were. Does
    >> anybody have a complete example in VB.NET (the whole aspx and aspx.vb
    >> file
    >> so I know I am including everything) that I can look at? Any help would
    >> be
    >> appreciated. Thanks.
    >> --
    >> Nathan Sokalski
    >>
    >> http://www.nathansokalski.com/
    >>
    >>
    >>
     
    Nathan Sokalski, Jul 16, 2005
    #5
  6. Here is an example. for the full context, see:
    http://www.eggheadcafe.com/articles/20040607.asp
    Hope this helps.
    --Peter

    using System;
    using System.Web;
    using System.Threading;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;

    namespace BlackbeltBLL {
    public class BackgroundService : IHttpModule {
    static Timer timer;
    int interval = 5000;
    public String ModuleName {
    get { return "BackgroundService"; }
    }

    public void Init(HttpApplication application) {
    // Wire-up application events
    if (timer == null)
    timer = new Timer(new
    TimerCallback(ScheduledWorkCallback),
    application.Context, interval, interval);
    }

    public void Dispose() {
    timer = null;
    }

    private void ScheduledWorkCallback (object sender) {
    HttpContext context = (HttpContext) sender;
    Poll(context);
    }

    void DoSomething (HttpContext context) {
    }

    #region DB Poll
    void Poll (HttpContext context) {
    SqlConnection connection = new
    SqlConnection(ConfigurationSettings.AppSettings["Northwind"]);
    SqlCommand command = new
    SqlCommand("SELECT * FROM changenotification", connection);
    SqlDataReader reader;
    string key =
    ConfigurationSettings.AppSettings["SqlDependency"];
    connection.Open();
    reader = command.ExecuteReader();
    while (reader.Read()) {
    string tableKey = String.Format(key, reader["Table"]);
    if (context.Cache[tableKey] != null) {
    int changeKey =
    int.Parse( context.Cache[ String.Format(key,
    reader["Table"])].ToString() );
    if (changeKey != int.Parse(
    reader["ChangeID"].ToString() ))
    context.Cache.Remove(tableKey);
    }
    }
    connection.Close();
    }
    #endregion
    }
    }


    Nathan Sokalski wrote:
    > I understand that that is true for the actual pages, but is there a way to
    > use a timer in the Global.asax.vb file? Doesn't that live the entire life of
    > the application? My basic goal is to find a way to periodically send myself
    > stats about what people do at my site, and send email newsletters. And there
    > is obviously some purpose for the System.Timers.Timer in ASP.NET since
    > Visual Studio lets you add it to an ASP.NET webform.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    > "Anand[MVP]" <> wrote in message
    > news:...
    > > Are you trying to use this in a ASP.NET Page? Pages live only for the time
    > > of
    > > the request. Once a request is served, the Page class is destroyed.
    > >
    > > --
    > > Rgds,
    > > Anand
    > > VB.NET MVP
    > > http://www.dotnetindia.com
    > >
    > >
    > > "Nathan Sokalski" wrote:
    > >
    > >> I am trying to learn how to use the System.Timers.Timer control to
    > >> perform
    > >> an action every certain amount of time. However, the Elapsed event
    > >> doesn't
    > >> want to fire, but I can't figure out why. I looked at several code
    > >> examples
    > >> online, but I think I was doing everything the same way they were. Does
    > >> anybody have a complete example in VB.NET (the whole aspx and aspx.vb
    > >> file
    > >> so I know I am including everything) that I can look at? Any help would
    > >> be
    > >> appreciated. Thanks.
    > >> --
    > >> Nathan Sokalski
    > >>
    > >> http://www.nathansokalski.com/
    > >>
    > >>
    > >>
     
    Peter Bromberg [C# MVP], Jul 17, 2005
    #6
    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. Daniel Maycock via .NET 247

    My Threading Timer Doesn't Fire

    Daniel Maycock via .NET 247, Mar 28, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    535
    steve
    Mar 30, 2005
  2. Roland
    Replies:
    1
    Views:
    533
    Rutger Smit
    Sep 8, 2004
  3. Nathan Sokalski

    Timer.Elapsed event doesn't want to fire

    Nathan Sokalski, Jul 15, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    4,924
    Peter Bromberg [C# MVP]
    Jul 17, 2005
  4. Nathan Sokalski

    Timer.Elapsed event doesn't want to fire

    Nathan Sokalski, Jul 15, 2005, in forum: ASP .Net Web Controls
    Replies:
    5
    Views:
    155
    Peter Bromberg [C# MVP]
    Jul 17, 2005
  5. rutherf
    Replies:
    2
    Views:
    454
    rutherf
    Oct 28, 2006
Loading...

Share This Page