本文介绍了类型'System.InvalidOperationException'的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨我有一个计时器,在过去的事件中,我想更改WindowState.但我收到此错误:
调用线程无法访问该对象,因为其他线程拥有它.
我应该怎么办?这是一个WPF项目
好的,这是我的代码
HiI have a timer which in Elapsed Event i want to change the WindowState. but i get this error:
The calling thread cannot access this object because a different thread owns it.
what should i do? it''s a WPF project
ok here is my code
System.Timers.Timer timer=new System.Timers.Timer();
private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
this.WindowState = System.Windows.WindowState.Minimized;
timer.Interval = 5 * 1000;
timer.Enabled = true;
timer.AutoReset = true;
timer.Start();
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
timer.Enabled = false;
this.WindowState = System.Windows.WindowState.Normal;
.....
timer.Enabled = true;
timer.Start();
}
catch (Exception ex)
{
Logger.Log("OnTimer Error:" + ex.Message);
}
}
推荐答案
这篇关于类型'System.InvalidOperationException'的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!