0

what i want to do is pause a video after every 10s the video should pause after ever 10s till the video ends

the code given below gives unexpected results the video pauses fine for the firs time (i.e after 10s) but when i play again it should pause after 10s but in my case it pauses randomly sometimes at 8s,3s 5s and etc what should i do?? please help thanks!!

void PlayClick(object sender, EventArgs e)
{
             VideoControl.Play();
            var dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 10);
            dispatcherTimer.Start();
 }

 private void dispatcherTimer_Tick(object sender, EventArgs e)
        {

            VideoControl.Pause();
        }
ahmad05
  • 438
  • 2
  • 6
  • 23

3 Answers3

1
  1. Add this in your dispatcherTimer_Tick-Method:

    dispatcherTimer.Stop();
    
  2. Move the following part into the constructor:

    dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
    dispatcherTimer.Interval = new TimeSpan(0, 0, 10);
    
  3. Make the DispatcherTimer a global variable.

EDIT: Thats how it should look like:

    class MyClass
    {
        private DispatcherTimer _dispatcherTimer; //now your dispatcherTimer is accessible everywhere in this class

        public MyClass()
        {
            _dispatcherTimer = new DispatcherTimer();
            _dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            _dispatcherTimer.Interval = new TimeSpan(0, 0, 10);
        }

        void PlayClick(object sender, EventArgs e)
        {
            VideoControl.Play();
            _dispatcherTimer.Start();
        }

        void dispatcherTimer_Tick(object Sender, EventArgs e)
        {
            _dispatcherTimer.Stop();
            VideoControl.Pause();
        }
    }
Florian Gl
  • 5,984
  • 2
  • 17
  • 30
  • how do i set Dispatcher time global?? when i put this code in class it gives error and if put this code in constructor than dispatertimer is not accessible in other method – ahmad05 Sep 26 '12 at 19:46
1

Bring the declaration of the timer out into a private class variable, move a couple lines to the constructor of the class, and stop the timer in the Tick handler.

The reason you don't want to keep creating the timer is because there are unmanaged resources involved with a timer and so you're closing that loop.

private dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); 

ctor
{
    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); 
    dispatcherTimer.Interval = new TimeSpan(0, 0, 10); 
}

void PlayClick(object sender, EventArgs e) 
{ 
    VideoControl.Play(); 
    dispatcherTimer.Start(); 
} 

private void dispatcherTimer_Tick(object sender, EventArgs e) 
{ 
    dispatchTimer.Stop();
    VideoControl.Pause(); 
} 
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • Note that starting a timer that is currently running will restart it from the beginning. That's what makes this code work properly if you press play, pause, and then play again real quick. – Servy Sep 26 '12 at 17:57
0

Try with following code in Tick event:

private void dispatcherTimer_Tick(object sender, EventArgs e)
    { 
        (sender as DispatcherTimer).Stop();
        VideoControl.Pause();
    }

You can make dispatcherTimer object outside Playclick event and only put Start() method inside PlayClick event in following way:

var dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
public Form1() //// form constructor where you are handling these all event....
{
     dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
     dispatcherTimer.Interval = new TimeSpan(0, 0, 10);
}

void PlayClick(object sender, EventArgs e)
{
            VideoControl.Play();
            dispatcherTimer .Start();
}
Akash KC
  • 16,057
  • 6
  • 39
  • 59
  • when i try to access dispatchertimer in PlayClick it gives the following error Error: The name 'dispatcherTimer' does not exist in the current context – ahmad05 Sep 26 '12 at 19:44
  • @ahmad05 : Sorry for late reply.....I've updated the answer....Go through it.....You can use sender object as Dispatcher timer object as I modified in PlayClick event..... – Akash KC Sep 27 '12 at 04:57
  • @LolCoder I dont think that the sender of PlayClick() is the DispatcherTimer. – Florian Gl Sep 27 '12 at 06:29
  • @FlorianGl : Thank you for figuring out.....Now, it's fixed....I've made DispatcherTimer object as global.... – Akash KC Sep 27 '12 at 07:03