Dav*_*lle 0 wpf multithreading timer
我有一个应用程序,我需要使用计时器切换一些控件的可见性.每5秒钟,一些控件会消失而另一些控件会出现.当我使用计时器时,它表示无法更改可见性,因为计时器线程不是控件的所有者.
我们怎么能绕过那个?
TKS
只需使用DispatcherTimer.每个tick上调用的代码自动在UI线程上运行.
例如.(来自MSDN)
// DispatcherTimer setup
var dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0,0,5);
dispatcherTimer.Start();
// System.Windows.Threading.DispatcherTimer.Tick handler
//
// Updates the current seconds display
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
// Updating the Label which displays the current second
lblSeconds.Content = DateTime.Now.Second;
}
Run Code Online (Sandbox Code Playgroud)