High Speed Timer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alexis4
    New Member
    • Dec 2009
    • 113

    High Speed Timer

    Hello!

    I need to slide a picture from right to left. So I need a timer event. When this event comes, I decrease picturebox’s X position by 1. The thing is that I need a timer faster than 1ms. After some searching, I came up with the following demo code (timer is set for 1ms):

    Code:
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Windows.Threading; //WindowsBase.dll
    
    namespace sliding_picture
    {
      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
        }
        public int msec_counter = 0;
        public DispatcherTimer disptimer = new DispatcherTimer();
    
        private void Form1_Load(object sender, EventArgs e)
        {
          disptimer.Interval = new TimeSpan(0, 0, 0, 0, 1);//1ms
          disptimer.Tick += new EventHandler(disptimer_Tick);
        }
            
        private void button1_Click(object sender, EventArgs e)
        {
          disptimer.IsEnabled = true;
          disptimer.Start();
        }
    
        private void disptimer_Tick(object sender, EventArgs e)
        {
          Point p = pictureBox1.Location;
          int x = p.X - 1;
          int y = p.Y;
          pictureBox1.Location = new System.Drawing.Point(x, y);
          textBox1.Text = pictureBox1.Location.ToString();
    
          msec_counter++;
          textBox2.Text = msec_counter.ToString();
        }
      }
    }
    When button1 is pressed the slider begins. At textbox1 I can see picturebox’s location and at picturebox2 I can see the time in ms.
    Unfortunately the event comes far slower than 1ms (approximately 1.5ms). I have tried to set timer interval at 1/10ms (0,0,0,0,1/10), but I can't get it faster than 1.5ms.

    Has anyone used a Dispatcher before? I need about 1/10ms tick...
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I've never used a Dispatcher before but out of curiosity, does the picture have to move at one pixel? One ms should be fast enough as an event interval, so if you want your picturebox to move faster, just increase the increment.

    Code:
        [B]private const int X_SPEED = 5;[/B]
        ...
        private void disptimer_Tick(object sender, EventArgs e)
        {
          Point p = pictureBox1.Location;
          [b]int x = p.X - X_SPEED;[/b]
          int y = p.Y;
          pictureBox1.Location = new System.Drawing.Point(x, y);
          textBox1.Text = pictureBox1.Location.ToString();
     
          msec_counter++;
          textBox2.Text = msec_counter.ToString();
        }

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      From MSDN:
      Timers are not guaranteed to execute exactly when the time interval occurs, but they are guaranteed to not execute before the time interval occurs. This is because DispatcherTimer operations are placed on the Dispatcher queue like other operations.

      Comment

      • alexis4
        New Member
        • Dec 2009
        • 113

        #4
        Thank you both for your response!

        I have also seen that tlhintoq, so I accept the fact that 1ms could raise to 1,5. But why am I not able to fall below 1ms? I have also seen that DateTime.Now clock can derive down to 100ns! The queue length is not letting me reach high speed? If so, can I use something else? Like let's say CPU ticks?
        I am not that interested in accuracy, my main concern is speed.

        I have also tried to increment pixels by 2 GaryTexmo. Unfortunately the lack of timer's accuracy makes the movement to seem jerky. This also happens with increment by 1 pixel, but it is much more smoother for the human eye.
        I know that the speed I'm asking is fast, but it could be achieved. I have seen sliders in windows, so I wander what clock was used by these applications...

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          Let's slow things down a moment.
          While you have your method chosen as a faster timer, let me ask you what your real GOAL is.

          If you are trying to get a faster yet smoother movement of a picturebox control, you can stop now. It won't matter how fast your timer is. The 'jerkiness' of the movement is just inherent in the types of controls you are using: Windows Forms and picturebox.

          Comment

          • alexis4
            New Member
            • Dec 2009
            • 113

            #6
            You are absolutely right tlhintoq! My goal is sliding letters, so I have just tried a sliding label instead of a picture and things gone better. I can now raise my step up to 2 pixels!
            But what about Windows Forms? I tried the sliding label on a parent picturebox, but the speed seems to be the same... Is there something I can do about this "Windows Forms" thing?

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              Is there something I can do about this "Windows Forms" thing?
              You'll have to be more specific about what you need and what problem you are facing. "This windows forms thing" leaves a lot of room for interpretation.

              Comment

              • alexis4
                New Member
                • Dec 2009
                • 113

                #8
                OK, forget about sliders, accuracy, jerkiness...

                Can I get a timer event faster than 1ms in .NET? I found fast timers but I did not see events triggered from their ticks. Is it possible to have a time triggered event every 1/10 ms? I don't care if it comes a bit later than that, all I care is to get a timer event of this frequency level.

                Comment

                • alexis4
                  New Member
                  • Dec 2009
                  • 113

                  #9
                  OK I got it!
                  I used a Stopwatch and inside a do loop I counted Stopwatch CPU ticks. Stopwatch seems to be really steady. Every 2ms l slide the message 1 pixel and it looks just fine!

                  Comment

                  Working...