0

I'm trying to make a function run every 200 milliseconds so that it can show the time difference between when the program first started and right now. I tried threading with this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ComputerTimer
{
    public partial class MainWindow : Window
    {
        private DateTime startTime, endTime;
        private static System.Timers.Timer timer1;
        private bool running = true;

        public MainWindow()
        {
            InitializeComponent();

            startTime = DateTime.Now;

            //makes new timer with 200 milliseconds interval
            timer1 = new System.Timers.Timer(200);
            timer1.Elapsed += new ElapsedEventHandler(timer1_Tick);
            timer1.Interval = 200;
            timer1.Enabled = true;

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            while (running)
            {
                endTime = DateTime.Now;
                TimeSpan span = endTime - startTime; //gets difference between now and when the program was started
                Title.Content = span.ToString().Substring(0, 8); //gets first 8 characters (taking out milliseconds)
            }
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            //when button is pressed to stop timer
            running = false;
        }
    }
}

But this just throws the exception 'InvalidOperationException' and says "Additional information: The calling thread cannot access this object because a different thread owns it." about line 48

Title.Content = span.ToString().Substring(0, 8); //gets first 8 characters (taking out milliseconds)

I'm quite confused what to do from here and have searched all over stack overflow looking for an answer but nothing seems to work. I have also tried DispatcherTimer but with no luck.

Edit: This is the answer which worked for me for anyone looking over this in the future

namespace ComputerTimer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private DateTime startTime, endTime;
        private DispatcherTimer dtClockTime;

        public MainWindow()
        {
            InitializeComponent();

            startTime = DateTime.Now;

            dtClockTime = new DispatcherTimer();

            dtClockTime.Interval = new TimeSpan(0, 0, 0, 0, 200); //in days, Hour, Minutes, Seconds, millis
            dtClockTime.Tick += dtClockTime_Tick;

            dtClockTime.Start();

        }

        private void dtClockTime_Tick(object sender, EventArgs e)
        {
            endTime = DateTime.Now;
            TimeSpan span = endTime - startTime; //gets difference between now and when the program was started
            Title.Content = span.ToString().Substring(0, 8);
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            //when button is pressed to stop timer
            dtClockTime.Stop();
        }
    }
}
Lachlan Mather
  • 135
  • 2
  • 5
  • 14

2 Answers2

0

You've created an endless loop in the timer Tick event, you shouldnt have a while(x) loop in there.

private void timer1_Tick(object sender, EventArgs e)
{
     endTime = DateTime.Now;
     TimeSpan span = endTime - startTime; //gets difference between now and when the program was started
     Title.Content = span.ToString().Substring(0, 8); //gets first 8 characters (taking out milliseconds)
 }

And your stop button should just disable the timer

private void btnStop_Click(object sender, RoutedEventArgs e)
{
     timer1.Enabled = false;
}

Edit: It might be that you need to set the SynchronisingObject of the timer

timer1.SynchronisingObject = this;

Failing the above it looks like for a wpf application (sorry, I initially missed the tag) you should be using a DispatcherTimer in place of System.Timers.Timer.

The setup is much the same as your existing code, it just uses a different type of timer which raises the tick event on the correct (UI) thread.


As an aside, there is no need to string mash a DateTime object, there are methods for being able to format a timespan

Title.Content = span.ToString("mm:ss.ffff");
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • It is still giving the same exception. – Lachlan Mather Sep 28 '16 at 09:58
  • 1
    @LachlanMather have you tried setting rhe [SynchronisingObject](https://msdn.microsoft.com/en-us/library/system.timers.timer.synchronizingobject(v=vs.110).aspx) to your form? – Jamiec Sep 28 '16 at 10:00
  • Sorry didn't read it lol. Where do I put the Synchronizing Object? – Lachlan Mather Sep 28 '16 at 10:04
  • @LachlanMather just in the setup of your timer, below where you set `Enabled` etc. Also, see my second update, that might be better for you. – Jamiec Sep 28 '16 at 10:07
  • Do you mean DispatchTimer or DispatcherTimer? If it's the later (I dont even know if the first one exists) I have tried that but it updated every second, 'DispatcherTimer dtClockTime = new DispatcherTimer(); dtClockTime.Interval = new TimeSpan(0, 0, 1); //in Hour, Minutes, Second. dtClockTime.Tick += dtClockTime_Tick; dtClockTime.Start();' – Lachlan Mather Sep 28 '16 at 10:09
  • Uhm... it updated every second because you told it to! (`new TimeSpan(0,0,1)`) – Jamiec Sep 28 '16 at 10:10
  • Yeah, they tend to hide that information in the [docs](https://msdn.microsoft.com/en-us/library/system.timespan.timespan(v=vs.110).aspx) (hint: [this one](https://msdn.microsoft.com/en-us/library/6c7z43tw(v=vs.110).aspx)) – Jamiec Sep 28 '16 at 10:13
  • But that document says that it works in Days, hours, minutes, seconds, millis so how come my previous example worked?? `TimeSpan(0,0,1);` – Lachlan Mather Sep 28 '16 at 10:16
  • Ok thanks it works now.. still have no idea how the `new TimeSpan(0,0,1);` thing works lol – Lachlan Mather Sep 28 '16 at 10:22
  • @LachlanMather there is more than one constructor on a `Timespan` one takes h,m,s and one takes d,h,m,s,ms – Jamiec Sep 28 '16 at 10:26
  • Thnx didn't know that :P – Lachlan Mather Oct 04 '16 at 06:37
0

Only add SynchronisingObject parameter for your timer.

Like this:

timer1.SynchronisingObject = this;