Timer.Elapsed event doesn't want to fire

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nathan Sokalski

    #1

    Timer.Elapsed event doesn't want to fire

    I am trying to learn how to use the System.Timers.T imer 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
    njsokalski@hotm ail.com
    有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。



  • Anand[MVP]

    #2
    RE: Timer.Elapsed event doesn't want to fire

    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



    "Nathan Sokalski" wrote:
    [color=blue]
    > I am trying to learn how to use the System.Timers.T imer 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
    > njsokalski@hotm ail.com
    > http://www.nathansokalski.com/
    >
    >
    >[/color]

    Comment

    • stand__sure

      #3
      Re: Timer.Elapsed event doesn't want to fire

      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)

      Comment

      • Rothariger

        #4
        RE: Timer.Elapsed event doesn't want to fire

        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:
        [color=blue]
        > I am trying to learn how to use the System.Timers.T imer 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
        > njsokalski@hotm ail.com
        > http://www.nathansokalski.com/
        >
        >
        >[/color]

        Comment

        • Nathan Sokalski

          #5
          Re: Timer.Elapsed event doesn't want to fire

          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.T imer in ASP.NET since
          Visual Studio lets you add it to an ASP.NET webform.
          --
          Nathan Sokalski
          njsokalski@hotm ail.com
          有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。

          "Anand[MVP]" <AnandMVP@discu ssions.microsof t.com> wrote in message
          news:EDD9C189-3F89-4A4D-8476-7373EC3A02B1@mi crosoft.com...[color=blue]
          > 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:
          >[color=green]
          >> I am trying to learn how to use the System.Timers.T imer 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
          >> njsokalski@hotm ail.com
          >> http://www.nathansokalski.com/
          >>
          >>
          >>[/color][/color]


          Comment

          • Peter Bromberg [C# MVP]

            #6
            Re: Timer.Elapsed event doesn't want to fire

            Here is an example. for the full context, see:

            Hope this helps.
            --Peter

            using System;
            using System.Web;
            using System.Threadin g;
            using System.Data;
            using System.Data.Sql Client;
            using System.Configur ation;

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

            public void Init(HttpApplic ation application) {
            // Wire-up application events
            if (timer == null)
            timer = new Timer(new
            TimerCallback(S cheduledWorkCal lback),
            application.Con text, interval, interval);
            }

            public void Dispose() {
            timer = null;
            }

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

            void DoSomething (HttpContext context) {
            }

            #region DB Poll
            void Poll (HttpContext context) {
            SqlConnection connection = new
            SqlConnection(C onfigurationSet tings.AppSettin gs["Northwind"]);
            SqlCommand command = new
            SqlCommand("SEL ECT * FROM changenotificat ion", connection);
            SqlDataReader reader;
            string key =
            ConfigurationSe ttings.AppSetti ngs["SqlDepende ncy"];
            connection.Open ();
            reader = command.Execute Reader();
            while (reader.Read()) {
            string tableKey = String.Format(k ey, reader["Table"]);
            if (context.Cache[tableKey] != null) {
            int changeKey =
            int.Parse( context.Cache[ String.Format(k ey,
            reader["Table"])].ToString() );
            if (changeKey != int.Parse(
            reader["ChangeID"].ToString() ))
            context.Cache.R emove(tableKey) ;
            }
            }
            connection.Clos e();
            }
            #endregion
            }
            }


            Nathan Sokalski wrote:[color=blue]
            > 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.T imer in ASP.NET since
            > Visual Studio lets you add it to an ASP.NET webform.
            > --
            > Nathan Sokalski
            > njsokalski@hotm ail.com
            > http://www.nathansokalski.com/
            > "Anand[MVP]" <AnandMVP@discu ssions.microsof t.com> wrote in message
            > news:EDD9C189-3F89-4A4D-8476-7373EC3A02B1@mi crosoft.com...[color=green]
            > > 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:
            > >[color=darkred]
            > >> I am trying to learn how to use the System.Timers.T imer 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
            > >> njsokalski@hotm ail.com
            > >> http://www.nathansokalski.com/
            > >>
            > >>
            > >>[/color][/color][/color]

            Comment

            Working...