简体   繁体   中英

C# InvalidOperationException when threading

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

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");

Only add SynchronisingObject parameter for your timer.

Like this:

timer1.SynchronisingObject = this;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM