[英]C# InvalidOperationException when threading
我試圖讓一個函數每 200 毫秒運行一次,以便它可以顯示程序首次啟動和現在之間的時間差。 我嘗試使用以下代碼進行線程化:
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;
}
}
}
但這只是拋出異常'InvalidOperationException'並說“附加信息:調用線程無法訪問此對象,因為不同的線程擁有它。” 關於第 48 行
Title.Content = span.ToString().Substring(0, 8); //gets first 8 characters (taking out milliseconds)
我很困惑從這里做什么,並在整個堆棧溢出中搜索以尋找答案,但似乎沒有任何效果。 我也嘗試過 DispatcherTimer 但沒有運氣。
編輯:這是對我有用的答案,適用於將來查看此問題的任何人
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();
}
}
}
您已經在計時器Tick
事件中創建了一個無限循環,您不應該在其中有一個while(x)
循環。
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)
}
你的停止按鈕應該只是禁用計時器
private void btnStop_Click(object sender, RoutedEventArgs e)
{
timer1.Enabled = false;
}
編輯:可能是您需要設置計時器的SynchronisingObject
timer1.SynchronisingObject = this;
如果上述情況失敗,對於 wpf 應用程序(對不起,我最初錯過了wpf標簽),您應該使用DispatcherTimer
代替System.Timers.Timer
。
設置與您現有的代碼大致相同,它只是使用不同類型的計時器,它在正確的(UI)線程上引發滴答事件。
順便說一句,不需要對DateTime
對象進行字符串混搭,有一些方法可以格式化時間跨度
Title.Content = span.ToString("mm:ss.ffff");
僅為您的計時器添加 SynchronisingObject 參數。
像這樣:
timer1.SynchronisingObject = this;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.