What is the best way to work with time-dependent events in WPF

I have a simple application in which there is a multimedia element and it plays several movies one by one. I want to have a delay of 15 seconds between one stop of the movie, and the next begins. I am new to WPF, and although I know how to do this using the old (WinForms) method using Timer and control.Invoke, I thought there should be a better way in WPF. There is?

+5
source share
1 answer

DispatcherTimer is what you want. In this case, you must create a DispatcherTimer with an interval of 15 seconds to start the first video. Then, when this video is completed, turn on the timer and in the checkmark event, show the next video, and set the timer to off so that it does not work 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 when the video is complete, set

yourTimer.Enabled = True;

and then in yourTimer.Tick event handler

private void yourTimer_Tick(object sender, EventArgs e)
{
    yourTimer.Enabled = False;//Don't repeat every 15 seconds
    nextVideo.Show();
}
+4
source

All Articles