I am currently stuck at a really strange problem with GDI and timers.
First the Code:
class Graph : UserControl {
private System.Threading.Timer timer;
private int refreshRate = 25; //Hz (redrawings per second)
private float offsetX = 0; //X offset for moving graph
public Graph() {
timer = new System.Threading.Timer(timerTick);
}
private void timerTick(object data) {
offsetX -= 1 / refreshRate;
this.Invalidate();
}
public void timerStart() {
timer.Change(0, 1000 / refreshRate);
}
private void onPaint(object sender, PaintEventArgs e) {
//350 lines of code for drawing the graph
//Here the offsetX will be used to move the graph
}
}
I am trying here to move a painted graph in a specific time to 1 "graph unit" to the left. So i use a timer which will change the offset in little steps, so it will be a smooth moving (thats the refreshRate for).
At the first view this code worked, but later i found following problem: If i am using a refreshRate of 1 (1Hz) it will just fine move my graph in 1 step 1 (graph unit) to the left. If i am increasing the refreshRate my movement will slow done. At 20 FPS its slighly slow, at 200 FPS its really slow..
So here is what i tried:
I used Refresh or Update instead of Invalidate
I used a normal Thread (with Sleep) instead of the timer
Both code changes didnt changed the result..
Beside the movement with the timer I also can move the graph with my mouse and if the timer is running i can still smoothly move the graph with my mouse. So its not a peformance problem..
I thought of a problem in the painting queue, because I am refreshing faster than the painting is done? (But why can I sill move the graph smoothly with my mouse?!)
So i need a little help here. Thanks