I have a simple app, that has media element in it, and it plays some movies, one after another. I want to have a 15 seconds delay between one movie stops playing, and the next one starts. I'm new to WPF and though I know how to do this the old (WinForms) way with a Timer and control.Invoke, I thought that there must be a better way in WPF. Is there?
+3
A:
The DispatcherTimer is what you want. In this instance you'd create a DispatcherTimer with an interval of 15 seconds, kick off the first video. Then when that video has completed enable the timer and in the tick event show the next video, and set the timer to disabled so it doesn't fire every 15 seconds. DispatcherTimer lives in the System.Windows.Threading namespace.
DispatcherTimer yourTimer = new DispatcherTimer();
yourTimer.Interval = new TimeSpan(0, 0, 15); //fifteen second interval
yourTimer.Tick += new EventHandler(yourTimer_Tick);
firstVideo.Show();
Assuming you have an event fired when the video is finished then set
yourTimer.Enabled = True;
and then in the yourTimer.Tick event handler
private void yourTimer_Tick(object sender, EventArgs e)
{
yourTimer.Enabled = False;//Don't repeat every 15 seconds
nextVideo.Show();
}
MrTelly
2009-02-10 11:12:41
does it execute on UI thread or do I have to do control.Invoke like with normal timer?
Krzysztof Koźmic
2009-02-10 11:17:39
thanks, that's exactly what I needed.
Krzysztof Koźmic
2009-02-10 11:36:30