The Problem
I'm trying to play a StoryBoard from a different thread but a 'System.InvalidOperationException' is thrown in WindowsBase.dll.
I have a custom Control, InfoBar
, which has a StoryBoard to close it and a System.Timers.Timer
object to do so after several seconds.
So, how do I call BeginStoryboard()
from a different thread?
Error Message
The exception thrown on BeginStoryboard(sb)
:
An exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll but was not handled in user code
Additional information: The calling thread cannot access this object because a different thread owns it.
My (simplified) Code
private int TimerSeconds;
private System.Timers.Timer t;
public InfoBar()
{
this.InitializeComponent();
TimerSeconds = 0;
t = new System.Timers.Timer(1000);
t.Elapsed += t_Elapsed;
}
void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if(TimerSeconds==3)
{
t.Stop();
TimerSeconds = 0;
System.Windows.Media.Animation.Storyboard sb = (System.Windows.Media.Animation.Storyboard)FindResource("sbClose");
BeginStoryboard(sb);
}
else
{
TimerSeconds++;
}
}