473,395 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

High Speed Timer

alexis4
113 100+
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):

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.Windows.Threading; //WindowsBase.dll
  5.  
  6. namespace sliding_picture
  7. {
  8.   public partial class Form1 : Form
  9.   {
  10.     public Form1()
  11.     {
  12.       InitializeComponent();
  13.     }
  14.     public int msec_counter = 0;
  15.     public DispatcherTimer disptimer = new DispatcherTimer();
  16.  
  17.     private void Form1_Load(object sender, EventArgs e)
  18.     {
  19.       disptimer.Interval = new TimeSpan(0, 0, 0, 0, 1);//1ms
  20.       disptimer.Tick += new EventHandler(disptimer_Tick);
  21.     }
  22.  
  23.     private void button1_Click(object sender, EventArgs e)
  24.     {
  25.       disptimer.IsEnabled = true;
  26.       disptimer.Start();
  27.     }
  28.  
  29.     private void disptimer_Tick(object sender, EventArgs e)
  30.     {
  31.       Point p = pictureBox1.Location;
  32.       int x = p.X - 1;
  33.       int y = p.Y;
  34.       pictureBox1.Location = new System.Drawing.Point(x, y);
  35.       textBox1.Text = pictureBox1.Location.ToString();
  36.  
  37.       msec_counter++;
  38.       textBox2.Text = msec_counter.ToString();
  39.     }
  40.   }
  41. }
  42.  
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...
Jan 2 '10 #1
8 12523
GaryTexmo
1,501 Expert 1GB
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.

Expand|Select|Wrap|Line Numbers
  1.     private const int X_SPEED = 5;
  2.     ...
  3.     private void disptimer_Tick(object sender, EventArgs e)
  4.     {
  5.       Point p = pictureBox1.Location;
  6.       int x = p.X - X_SPEED;
  7.       int y = p.Y;
  8.       pictureBox1.Location = new System.Drawing.Point(x, y);
  9.       textBox1.Text = pictureBox1.Location.ToString();
  10.  
  11.       msec_counter++;
  12.       textBox2.Text = msec_counter.ToString();
  13.     }
Jan 2 '10 #2
tlhintoq
3,525 Expert 2GB
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.
Jan 2 '10 #3
alexis4
113 100+
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...
Jan 2 '10 #4
tlhintoq
3,525 Expert 2GB
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.
Jan 2 '10 #5
alexis4
113 100+
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?
Jan 2 '10 #6
tlhintoq
3,525 Expert 2GB
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.
Jan 2 '10 #7
alexis4
113 100+
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.
Jan 3 '10 #8
alexis4
113 100+
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!
Jan 10 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Alexander Muylaert | last post by:
Hi Does anyone know a good starting point about high speed string processing in C#? What I need is a very fast routine for a case insensitive "contains". e == E == é == ë == ... Kind...
1
by: Nirupam Gupta | last post by:
Hi I have been assigned a Project to develope the interface of DLP Projector the interface should be able to control the functions of Projector lik Contrast Brightness, Volume Mute,
4
by: David Pendrey | last post by:
Greetings, I am writing an application which will need to move around 50 bitmaps on the form at a speed high enough that it appears smooth. I am currently using some custom controls to contain the...
4
by: Dmitri Sologoubenko | last post by:
Hi, guys! I need a C++ class (Linux/POSIX/GNU C++), which can be started and stopped, and calls a virtual callback instance method (e.g. "expired()") when a given time has elapsed. High...
19
by: Jon Slaughter | last post by:
Is it possible to have a synchronous thread... actually a timer that is beyond the 1khz/1ms resolution that .NET offers? I want to poll the parallel port at rates beyond 1khz... about 10 to 100...
7
by: Andrew Wan | last post by:
I found this excellent High Speed Timer (in Pascal). I compiled it (using Turbo Pascal 7 and it runs fine): http://www.sorucevap.com/bilisimteknolojisi/programcilik/pascal/ders.asp?207995 and...
0
by: =?GB2312?B?zPC5zw==?= | last post by:
Howdy, I use python2.5 in WindowsXP. If using time.time() as timer, it seems the maximum precision is about 10-12ms. Maybe this is caused by the time slice defined in process scheduler. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.