Hello,
As there is no Timer control item in the WPF I have used DispatcherTimer class in my code to show the countdown that starts from 1 and ticks every second.....but what I want to do and where I am having problem is that with Minute and second time display pannel I want to display a fast changing time beshide second like 00:00:XX "XX" being fast changing value.....
Here is what I have done till now:
Code:
int a = 1;
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
timerTextblock.Text = a.ToString()+" : "+DateTime.Now.Millisecond.ToString();
a++;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer.Start();
}
please note that i need not to be that accurate under the second, I just want it visually there as it gives some sense of urgency........
no this code works fine..but i want other fast changing time after second as it gives sense of urgency....I could do it with 2 timers displayed at 2 different textblock bu not with one timer.....
Last edited by rocky_upadhaya; April 8th, 2010 at 07:35 AM.
no this code works fine..but i want other fast changing time after second as it gives sense of urgency....I could do it with 2 timers displayed at 2 different textblock bu not with one timer.....
yes I am but when i used millisecond it is too fast for user to see..... I felt comfortable with
time span of
Code:
dispatcherTimer.Interval = new TimeSpan(1000000);
But new problem arised...while using this time span which was faster than second and comparatively easier to see but was always 3-8 seconds faster or slower on each minute....It simply meant I couldn't control that time span......
Thank you for the code but i think you didn't got my question.....the programs requires start timer from 0 second and display in minute:seconf:ff format .....while using this code:
I want to display it in minute:seconf:ff format but I want the value of realSec at the end of the window......for example if the time elasped is 2 min 3 second XX ff then I want exact value of 123 sec.....
The only problem that is on my code is that I was unable to update the second after exact value of ff........
Code:
int realSec=1;
int microSec=1;
private void milliSecTimer_Tick(object sender, EventArgs e)
{
TimerTextblock.Text = realSec.ToString()+" : "+microSec.ToString();
microSec++;
if (microSec > 100) //please help me in this loop because this loop controls the value of second.......
{
microSec = 0;
realSec++;
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
DispatcherTimer milliSecTimer = new DispatcherTimer();
milliSecTimer.Tick += new EventHandler(milliSecTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0,1);
milliSecTimer.Start();
}
Please Help me on the loop that I have commented because I think this loop is fully controls the increase rate of real sec....
Last edited by rocky_upadhaya; April 9th, 2010 at 07:03 AM.
Rocky, do you want any sort of accuracy with your code?
If you do, then your approach is flawed because you are introducing error each time through the timer loop (i.e. consider that it takes extra time to run the calculations and the system doesn't guarantee the timer will be precisely fired).
Consider using a method that simply uses the timer for UI updates but the actual elapsed time is tracked by a component that uses the system clock. This way, the UI may not be precisely updated but the end time will be very close to what you've specified.
Given this, see if the System.Diagnostic.Stopwatch class can help you.
That is the whole point...Thank you for recognising my exact problem of inaccurate timer.....Can I update stopwatch's value in Timer event because I want time on background precisely and on the UI as well........I will try the Stopwatch method.....Thank you
Bookmarks