Results 1 to 3 of 3

Thread: monitoring elapsed time with the stopwatch

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    monitoring elapsed time with the stopwatch

    Hello,

    I am developing a windows service in VS 2005 that will monitor time and do something every 5 seconds.

    I have created some code using the stopwatch class. Can anyone tell me if this is the best way to do this .

    Code:
    protected override void OnStart(string[] args)
            {
                // TODO: Add code here to start your service.
                sw.Start();
                this.RunningTime();
            }
    
            protected override void OnStop()
            {
                // TODO: Add code here to perform any tear-down necessary to stop your service.
                sw.Stop();
            }
    
            private void RunningTime()
            {
                if (sw.IsRunning)
                {
                    if (sw.Elapsed.Seconds == 5)
                    {
                        //Do something here
                    }
                }
            }
    Many thanks in advance,

    Steve
    steve

  2. #2
    Lively Member
    Join Date
    Oct 2006
    Posts
    71

    Re: monitoring elapsed time with the stopwatch

    It looks like that code won't really do anything at all. Right away you check if the elasped time is 5 seconds, which it won't be, and that's it. If you want to do something every 5 seconds, probably a server based timer is the way to go. Introduction to Server-Based Timers

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: monitoring elapsed time with the stopwatch

    As Buoy suggests, this is not a job for a Stopwatch. A Stopwatch is supposed to tell you how much time has elapsed when you ask, not automatically tell you when a specific amount of time has elapsed. With a Stopwatch you start it, do something, then check how long it took. You should indeed be using a Timer.

    Only the Windows.Forms.Timer is in the VS 2005 Toolbox by default. You want to use a Timers.Timer. You can add it to the Toolbox manually and then add one to your Service in the designer or you can create one in code. If you do it in code make sure you declare the variable WithEvents.

    Basically you would set the Interval to 5000 (milliseconds) and then handle the Elapsed event. You would perform your action in the Elapsed event handler. By default that event will only be raised once. If you set the AutoReset property to True then it will be raised repeatedly on the specified Interval until you explicitly Stop it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width