Hi there,

I have two WPF windows. The first (Window1) has a timer that moves an item down a list, then passes the new current selection to a global variable I defined in App.xaml.cs

Window 2 then has another timer that is set to .1 of a second after the timer in Window 1, and it uses this to grab the new current selection.

This works. Mostly. Because there is a delay between when Window 1 timer starts and when Window 2 timer starts, the two timers eventually go out of sync over a long period of time and bad things start happening with my code.

What I would like to do, is when I show Window 2, is to somehow 'reset' whatever value the Window 1 timer is at. This will not solve the problem, since the Window 1 timer is always smaller than the Window 2 timer, however, because the difference between them is so minute (0.001 seconds), the amount of time it would take for the timers to go too badly out of sync with each other would make it an acceptable solution.

The only issue is this strikes me as a really patchwork solution. But I can't see any other solution. If I were somehow able to create an event in App.xaml.cs that my two windows could use to see if the global variable changed, that would be ideal, but I don't think that's possible.

So does anyone know how I might reset the value of a timer?

Recommended Answers

All 2 Replies

Have you tried using one timer? With the interval set to the timer1 interval, you can use the modulus operator(modulo the interval) to fire the first window when it is 0 and fire the second window when it is 100. With an interval set to 1000 the timer will fire window1 every second and window2 one tenth of a second later. Latency might create a temporary out of sync situation but it should correct itself on the next cycle.

Without the actual code these numbers may not be 100 percent accurate but the principle should still work.

No, the problem is having a timer that writes to a global variable, and a different timer that reads that variable. Because they are in different windows, I didn't think I was able to have them share a timer??

I think I've created a workaround. Instead of setting the second time to 0.001 seconds later, I've simply set it to the same time as timer 1. Because the operation is so tiny, it seems to processes both at the same time. Not an ideal solution if that code gets bigger going forward though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.