4

I need to show a running timer on the window along with information of a test, such as ID, test name, status, start time, end time, etc.

I really wish that I could have a timer control on the page that tells the user that how long the test has been running.


How would one add a running timer on the page?

In addition, if this is possible, I wish my timer could start from some specific time instead of 00:00:00. The reason I need this is because the user can open this page when the test has been running for a while, and the elapsed time shown on the timer should be (current_time - start_time) and start from here.

If the test start at: 7:00 AM and the user opens the page at 7:05AM and the test is still running, the timer should start from 00:05:00.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
KField
  • 151
  • 1
  • 4
  • 13
  • Providing requirements without showing any effort was once considered [off-topic](http://stackoverflow.com/help/on-topic). It is still [being debated now](http://meta.stackexchange.com/questions/215596/are-code-questions-without-an-attempt-now-on-topic), and many people consider it to be rude. – Scott Solmer Jul 23 '14 at 22:37
  • I'll still throw you a bone though, you're looking for a [DispatcherTimer](http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer%28v=vs.110%29.aspx) – Scott Solmer Jul 23 '14 at 22:39
  • Thanks, Okuma. I tried to use DispatcherTimer in my app but I don't know how to show it on the page. I don't know which tool I can use to show the DispatcherTimer. – KField Jul 23 '14 at 22:46
  • The timer itself isn't a control, it does not get shown. You need to do something with the tick events to update some UIElement. Whether that be a Label, Text Block, Progress Bar, or whatever. – Scott Solmer Jul 23 '14 at 22:52

2 Answers2

13

Here is a very basic example I threw together.

using System.Windows.Threading;

namespace BasicTimer
{
    public partial class MainWindow : Window
    {
        DispatcherTimer t;
        DateTime start;
        public MainWindow()
        {
            InitializeComponent();
            t = new DispatcherTimer(new TimeSpan(0, 0, 0, 0, 50), DispatcherPriority.Background,
                t_Tick, Dispatcher.CurrentDispatcher); t.IsEnabled = true;
            start = DateTime.Now;
        }

        private void t_Tick(object sender, EventArgs e)
        {
            TimerDisplay.Text = Convert.ToString(DateTime.Now - start);
        }

MainWindow XAML

<Window x:Class="BasicTimer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="100" Width="200">
    <Grid>
        <TextBlock x:Name="TimerDisplay" HorizontalAlignment="Left"/>
    </Grid>
</Window>
Scott Solmer
  • 3,871
  • 6
  • 44
  • 72
  • The event handler has to be static, preventing access to your `TimerDisplay`. You have to use a different constructor and then subscribe to `DispatcherTimer.Tick` for this. – Isaac Baker Jun 27 '16 at 01:51
0

Here is how I achieved this.

Stopwatch watch = new Stopwatch();

private void StartTimer()
{
    new Thread(() =>
    {
        watch.Restart();
        while (watch.IsRunning)
        {
            Dispatcher.Invoke(() =>
            {
                timeText.Text = Math.Round(watch.Elapsed.TotalSeconds, 2).ToString() + "s";
            });
        }

    }).Start();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    //i had to start and stop the timer inside a thread, i was having issues without doing so
    new Thread(() =>
    {
        StartTimer();
        //I was calling an api here
        Dispatcher.Invoke(() =>
        {
            messageText.Text = response.message;
        });
        watch.Stop();
    }).Start();            
}
salman_sali
  • 181
  • 2
  • 8