几个示例,使用 System.Windows.Forms.Timer 替换您现在使用的 System.Timers.Timer。
后者在 ThreadPool 线程中引发其 Elapsed 事件。与许多其他对象一样,DataBindings 不能跨线程工作。
您可以在此处阅读其他详细信息:
Why does invoking an event in a timer callback cause following code to be ignored?
System.Windows.Forms.Timer 已经同步,它的Tick 事件在 UI 线程中引发。
使用此计时器的新模型类:
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
public class Model : INotifyPropertyChanged, IDisposable
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
private readonly Timer timer;
private int counter;
public Model() {
counter = 0;
timer = new Timer() { Interval = 1000 };
timer.Tick += this.Timer_Elapsed;
StartTimer();
}
public int Counter {
get => counter;
set {
if (counter != value) {
counter = value;
NotifyPropertyChanged();
}
}
}
public void StartTimer() => timer.Start();
public void StopTimer() => timer.Stop();
private void Timer_Elapsed(object sender, EventArgs e) => Counter++;
public void Dispose(){
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing) {
lock (this) {
if (timer != null) timer.Tick -= Timer_Elapsed;
timer?.Stop();
timer?.Dispose();
}
}
}
}
如果您想使用System.Threading.Timer,您需要将其Elapsed 事件与UI 线程同步,因为如上所述,PropertyChanged 通知无法跨线程编组,您的DataBindings 将无法工作。
您可以(主要)使用两种方法:
注意:您不能将 Model 对象声明为 Field 并同时进行初始化:在初始化起始 Form 之前没有 SynchronizationContext。您可以在表单的构造函数中或之后的任何时间初始化一个新实例:
public partial class Form1 : Form
{
Model model = new Model(); // <= Won't work
// ------------------------------------------
Model model = null; // <= It's OK
public Form1()
{
InitializeComponent();
// Using the SynchronizationContext
model = new Model();
// Or, using A Synchronizing Object
model = new Model(this);
var binding = new Binding("Text", model, "Counter", true, DataSourceUpdateMode.OnPropertyChanged);
label1.DataBindings.Add(binding);
}
}
使用两者的修改后的模型类:
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Timers;
public class Model : INotifyPropertyChanged, IDisposable
{
public event PropertyChangedEventHandler PropertyChanged;
internal readonly SynchronizationContext syncContext = null;
internal ISynchronizeInvoke syncObj = null;
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
private System.Timers.Timer timer;
private int counter;
public Model() : this(null) { }
public Model(ISynchronizeInvoke synchObject)
{
syncContext = SynchronizationContext.Current;
syncObj = synchObject;
timer = new System.Timers.Timer();
timer.SynchronizingObject = syncObj;
timer.Elapsed += Timer_Elapsed;
StartTimer(1000);
}
public int Counter {
get => counter;
set {
if (counter != value) {
counter = value;
NotifyPropertyChanged();
}
}
}
public void StartTimer(int interval) {
timer.Interval = interval;
timer.AutoReset = true;
timer.Start();
}
public void StopTimer(int interval) => timer.Stop();
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (syncObj is null) {
syncContext.Post((spcb) => Counter += 1, null);
}
else {
Counter += 1;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing) {
lock (this) {
if (timer != null) timer.Elapsed -= Timer_Elapsed;
timer?.Stop();
timer?.Dispose();
}
}
}
}