473,765 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

High Speed Timer

alexis4
113 New Member
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 12679
GaryTexmo
1,501 Recognized Expert Top Contributor
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 Recognized Expert Specialist
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 New Member
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 Recognized Expert Specialist
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 New Member
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 Recognized Expert Specialist
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 New Member
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 New Member
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
4736
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 regards
1
1994
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
1422
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 bitmap and the logic for the movement in the same location and have have a loop which updates all bitmaps each 50ms (20fps). Unfortunatly the time taken to render this is too great and my application does not function correctly. The solution does...
4
5407
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 resolution means that expire time should be measured in microseconds, or at least in milliseconds. I had a look on Gnu C <sys/time.h>'s functions getitimer and setitimer, but these all use a signal (SIGALRM) to notify expiration, and I don't know the...
19
3447
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 times that rate if possible. Essnetially I want to monitor the parallel port for data and display it but I need a fast but somewhat precise way of knowing the sample rate. Doing some tests on just a simple thread I can get speeds at around...
7
502
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 same High Speed Timer here too: http://groups.google.com/group/comp.lang.pascal/browse_thread/thread/92e9398f16c10ba4/e67ff3cf587648ef?lnk=st&q=inline(%24CD)+inline(%241C)+inline(%249C)&rnum=1&hl=en#e67ff3cf587648ef I converted it to C (using p2c),...
0
1138
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, in my project, I have to get timer percision up to 1ms. What should I do? Do I have to call Win32 API? Or simply promote python thread priority? Regards,
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10160
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9951
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.