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):
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...
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();
}
}
}
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...
Comment