You can do the same thing with System.Timers.Timer
. Note that it has different (but similar) methods and properties than the DispatcherTimer
.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.SizeChanged += MainWindow_SizeChanged;
_resizeTimer.Elapsed += _resizeTimer_Elapsed;
}
private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
{
_resizeTimer.Stop();
_resizeTimer.Start();
}
System.Timers.Timer _resizeTimer = new System.Timers.Timer { Interval = 1500 };
void _resizeTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
_resizeTimer.Stop();
System.Diagnostics.Debug.WriteLine("SizeChanged end");
//Do end of resize processing
//you need to use BeginInvoke to access the UI elements
//this.Dispatcher.BeginInvoke(...)
}
}
One thing is more important, that System.Timers.Timer
runs on a different thread other than the UI thread, which means you can't directly manipulate UI elements from within the _resizeTimer_Elapsed
method. You need to use BeginInvoke
to marshal the call back to the UI thread.
Find the differences between System.Timers.Timer
and DispatcherTimer
. There are many questions and answers on this subject. I just want to show you how to do it with the classic timer.