-
April 1st, 2010, 09:33 AM
#1
Timer Problem in WPF.....
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........
-
April 6th, 2010, 02:33 PM
#2
Re: Timer Problem in WPF.....
What is the problem? Is the dispatcherTimer_Tick handler not getting called (did you verify this by putting a breakpoint inside the handler)?
-
April 8th, 2010, 07:31 AM
#3
Re: Timer Problem in WPF.....
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.
-
April 8th, 2010, 12:15 PM
#4
Re: Timer Problem in WPF.....
Originally Posted by rocky_upadhaya
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.....
Are you familiar with Msdn?
http://msdn.microsoft.com/en-us/libr....timespan.aspx
You know there is another timespan constructor.
Code:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1);
dispatcherTimer.Start();
}
-
April 8th, 2010, 12:33 PM
#5
Re: Timer Problem in WPF.....
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......
-
April 8th, 2010, 01:02 PM
#6
Re: Timer Problem in WPF.....
The following works fine:
Code:
publicpartialclassWindow1 : Window
{
public Window1( )
{
InitializeComponent( );
}
privatevoid Window_Loaded( object sender, RoutedEventArgs e )
{
_dispatcherTimer.Tick +=new System.EventHandler(OnTimer);
_dispatcherTimer.Interval = newTimeSpan(100);
_dispatcherTimer.Start( );
}
privatevoid OnTimer( object sender, EventArgs e)
{
_timerTextBlock.Text = DateTime.Now.ToString( "HH:mm:ss:ff" );
}
privatereadonlyDispatcherTimer _dispatcherTimer = new DispatcherTimer( );
}
-
April 8th, 2010, 08:43 PM
#7
Re: Timer Problem in WPF.....
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:
Code:
private void milliSecTimer_Tick(object sender, EventArgs e)
{
TimerTextblock.Text = realSec.ToString()+" : "+microSec.ToString();
microSec++;
if (microSec > 10)
{
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, 1);
milliSecTimer.Interval = new TimeSpan(1000000);
milliSecTimer.Start();
}
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.....
-
April 8th, 2010, 09:21 PM
#8
Re: Timer Problem in WPF.....
So set the textbox when you reach the final time value and stop the timer.
In order to have the millisecond fields update quickly, your timer is going to need to be fast.
So within the timer loop, do the math as necessary to get the realSec resolution.
The problem doesn't seem that hard, unless you're leaving something out.
If that doesn't do it for you, then post a list of the actual values you would to see.
Last edited by Arjay; April 8th, 2010 at 09:27 PM.
-
April 8th, 2010, 09:49 PM
#9
Re: Timer Problem in WPF.....
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.
-
April 12th, 2010, 04:29 PM
#10
Re: Timer Problem in WPF.....
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.
-
April 12th, 2010, 07:40 PM
#11
Re: Timer Problem in WPF.....
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|