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): -
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...
8 12523
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. - private const int X_SPEED = 5;
-
...
-
private void disptimer_Tick(object sender, EventArgs e)
-
{
-
Point p = pictureBox1.Location;
-
int x = p.X - X_SPEED;
-
int y = p.Y;
-
pictureBox1.Location = new System.Drawing.Point(x, y);
-
textBox1.Text = pictureBox1.Location.ToString();
-
-
msec_counter++;
-
textBox2.Text = msec_counter.ToString();
-
}
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.
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...
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.
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?
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.
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.
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!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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,
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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,...
| |