Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Timer tick event count

Timer tick event count

Scheduled Pinned Locked Moved C#
helpdata-structuresworkspace
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mprice214
    wrote on last edited by
    #1

    Hi all, I've been trying to solve this problem for over an hour and as I'm a rookie I know that what I want to do is easy to implement, but my lack of experience is preventing me from doing so. With that said, I have some code for a ZedGraph dynamically updating control. Notice that the variable double time controls the x Scale. This code is executed in a timer tick event in order to draw the line on the graph. However, as the x-scale is driven by Environment.Tickcount (which is totally independent of the timer) the graph x-axis increments regardless of whether the timer is enabled or not. I want the double time to be replaced with a double that is based upon the elapsed time of the timer and not the Environment.TickCount, but I can't figure this out. Maybe because I'm too tired... :) Thanks in advance for any help.

    //make sure curvelist has one curve
    if (zedGraphControl1.GraphPane.CurveList.Count <= 0)
    return;
    //Get first CurveItem in Graph
    LineItem curve = zedGraphControl1.GraphPane.CurveList[0] as LineItem;
    if (curve == null)
    return;
    // Get the PointPairList
    IPointListEdit list = curve.Points as IPointListEdit;
    // If this is null, it means the reference at curve.Points does not
    // support IPointListEdit, so we won't be able to modify it
    if (list == null)
    return;

            // Time is measured in seconds
                            
            double time = (Environment.TickCount - tickStart) / 1000.0;
    
                     
            // 3 seconds per cycle
            list.Add(time, dblLabel);
    
            // Keep the X scale at a rolling 30 second interval, with one
            // major step between the max X value and the end of the axis
            Scale xScale = zedGraphControl1.GraphPane.XAxis.Scale;
            if (time > xScale.Max - xScale.MajorStep)
            {
                xScale.Max = time + xScale.MajorStep;
                xScale.Min = xScale.Max - 30.0;
            }
    
            // Make sure the Y axis is rescaled to accommodate actual data
            zedGraphControl1.AxisChange();
            // Force a redraw
            zedGraphControl1.Invalidate();
    
    OriginalGriffO 1 Reply Last reply
    0
    • M mprice214

      Hi all, I've been trying to solve this problem for over an hour and as I'm a rookie I know that what I want to do is easy to implement, but my lack of experience is preventing me from doing so. With that said, I have some code for a ZedGraph dynamically updating control. Notice that the variable double time controls the x Scale. This code is executed in a timer tick event in order to draw the line on the graph. However, as the x-scale is driven by Environment.Tickcount (which is totally independent of the timer) the graph x-axis increments regardless of whether the timer is enabled or not. I want the double time to be replaced with a double that is based upon the elapsed time of the timer and not the Environment.TickCount, but I can't figure this out. Maybe because I'm too tired... :) Thanks in advance for any help.

      //make sure curvelist has one curve
      if (zedGraphControl1.GraphPane.CurveList.Count <= 0)
      return;
      //Get first CurveItem in Graph
      LineItem curve = zedGraphControl1.GraphPane.CurveList[0] as LineItem;
      if (curve == null)
      return;
      // Get the PointPairList
      IPointListEdit list = curve.Points as IPointListEdit;
      // If this is null, it means the reference at curve.Points does not
      // support IPointListEdit, so we won't be able to modify it
      if (list == null)
      return;

              // Time is measured in seconds
                              
              double time = (Environment.TickCount - tickStart) / 1000.0;
      
                       
              // 3 seconds per cycle
              list.Add(time, dblLabel);
      
              // Keep the X scale at a rolling 30 second interval, with one
              // major step between the max X value and the end of the axis
              Scale xScale = zedGraphControl1.GraphPane.XAxis.Scale;
              if (time > xScale.Max - xScale.MajorStep)
              {
                  xScale.Max = time + xScale.MajorStep;
                  xScale.Min = xScale.Max - 30.0;
              }
      
              // Make sure the Y axis is rescaled to accommodate actual data
              zedGraphControl1.AxisChange();
              // Force a redraw
              zedGraphControl1.Invalidate();
      
      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Just set up:

      private double myTime = 0.0;

      as a class field. In your Load event or constructor:

      Timer tim = new Timer();
      tim.Interval = 1000;
      tim.Tick += new new EventHandler(timer_Tick);
      tim.Start();

      And add the Tick event handler:

      void timer_Tick(object sender, EventArgs e)
      {
      myTime+= 1.0;
      }

      However, it may be easier and better to save the start time as a DateTime; and subtract it from DateTime.Now in your update method. You can then get the elapsed seconds from the resulting TimeSpan.

      You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      M 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Just set up:

        private double myTime = 0.0;

        as a class field. In your Load event or constructor:

        Timer tim = new Timer();
        tim.Interval = 1000;
        tim.Tick += new new EventHandler(timer_Tick);
        tim.Start();

        And add the Tick event handler:

        void timer_Tick(object sender, EventArgs e)
        {
        myTime+= 1.0;
        }

        However, it may be easier and better to save the start time as a DateTime; and subtract it from DateTime.Now in your update method. You can then get the elapsed seconds from the resulting TimeSpan.

        You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

        M Offline
        M Offline
        mprice214
        wrote on last edited by
        #3

        Thank you for the help!

        OriginalGriffO 1 Reply Last reply
        0
        • M mprice214

          Thank you for the help!

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          You're welcome!

          You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups