# Other .NET Programming > Windows Presentation Foundation (WPF) & XAML forum >  Timer Problem in WPF.....

## rocky_upadhaya

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:


```

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........

----------


## Arjay

What is the problem?  Is the dispatcherTimer_Tick handler not getting called (did you verify this by putting a breakpoint inside the handler)?

----------


## 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.....

----------


## Arjay

> 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.


```
 
 
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();
}
```

----------


## rocky_upadhaya

yes I am but when i used millisecond it is too fast for user to see..... I felt comfortable with
time span of 


```
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......

----------


## Arjay

The following works fine:



```
 
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( );

}
```

----------


## rocky_upadhaya

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:


```
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.....

----------


## Arjay

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.

----------


## rocky_upadhaya

The only problem that is on my code is that I was unable to update the second after exact value of ff........


```
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....

----------


## Arjay

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.

----------


## rocky_upadhaya

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

----------

