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();
}
Add this in your dispatcherTimer_Tick-Method:
dispatcherTimer.Stop();
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);
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();
}
}
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();
}
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();
}
Related
I want to change the interval of my dispatcher time in run time
here is my code :
InitializeComponent();
DispatcherTimer messageTimer = new DispatcherTimer();
messageTimer.Tick += new EventHandler(messageTimer_Tick);
messageTimer.Interval = TimeSpan.FromSeconds(1);
messageTimer.Start();
idk how to change the interval of the DispatcherTimer at run time
I created two textblocks and a button.
textblock : x:Name="txt_Count"
textblock : x:Name="txt_TimeNow"
Button : x:Name="btn_changeTime_s"
int count = 0;
DispatcherTimer messageTimer;
DispatcherTimer TimeNow;
public MainWindow()
{
InitializeComponent();
messageTimer = new DispatcherTimer();
messageTimer.Tick += new EventHandler(messageTimer_Tick);
messageTimer.Interval = TimeSpan.FromSeconds(1);
messageTimer.Start();
TimeNow = new DispatcherTimer();
TimeNow.Tick += new EventHandler(TimeNow_Tick);
TimeNow.Interval = TimeSpan.FromMilliseconds(100);
TimeNow.Start();
}
private void TimeNow_Tick(object sender, EventArgs e)
{
txt_now.Text = DateTime.Now.ToString();
}
private void messageTimer_Tick(object sender, EventArgs e)
{
txt_Count.Text = count.ToString();
count++;
}
private void btn_changeTime_s_Click(object sender, RoutedEventArgs e)
{
messageTimer.Interval = TimeSpan.FromSeconds(2);
}
If you need to modify the Main UI Thread in the background, use the syntax
txt_Count.Dispatcher.Invoke(DispatcherPriority.Normal,new Action(delegate ()
{
txt_Count.Text = count.ToString();
}
));
I have some MediaElements in my WPF-application
If i call Question1_5.Play(); then Timer_activated.Play(); does not play on time (this audio plays 5-6 seconds after calling Play() method).
What the problem?
private void NewGame_MouseLeftButtonDown(object sender,
MouseButtonEventArgs e) {
Dispatcher.BeginInvoke((Action)(() => Windows.SelectedItem = Game));
Questions1_5.Play();
sb = FindResource("TimerAppear") as Storyboard;
sb.Begin();
sb.Pause();
timer = new DispatcherTimer();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = new TimeSpan(0, 0, 4);
timer.Start();
}
private void timer_Tick(object sender, EventArgs e) {
Timer_activated.Play();
sb.Resume();
sb = FindResource("Seconds15") as Storyboard;
timer.Stop();
}
How can I implement a countdown timer in my Windows Phone 8.1 app? There seems to be no information available for it. All I could find works for either a Windows Forms application or a Windows Application, but none of them seems to be working on the phone app.This is what I am doing-
namespace Timer
{
public partial class MainPage : Page
{
DispatcherTimer mytimer = new DispatcherTimer();
int currentcount = 0;
public MainPage()
{
InitializeComponent();
mytimer = new DispatcherTimer();
mytimer.Interval = new TimeSpan(0, 0, 0, 1, 0);
mytimer.Tick += new EventHandler(mytime_Tick);
//HERE error comes Cannot implicitly convert type System.EventHandler to System.EventHandler<object>
}
private void mytime_Tick(object sender,EventArgs e)
{
timedisplayBlock.Text = currentcount++.ToString();
}
private void startButton_Click(object sender, RoutedEventArgs e)
{
mytimer.Start();
}
}
}
But it gives me this error-
Cannot implicitly convert type System.EventHandler to System.EventHandler<object>
Should your increment not be written as
timedisplayBlock.Text = (++currentcount).ToString();
Or
currentcount++;
timedisplayBlock.Text = currentcount.ToString();
I don't think the increment will matter too much but still should be written correctly to ensure you do not leave yourself a count behind.
- See Sergiol's issue with David's answer on the link below https://stackoverflow.com/a/7848129/2110465
The other thing i have noticed is that you are initializing the DispatcherTimer twice...
DispatcherTimer mytimer = new DispatcherTimer();
...
..
mytimer = new DispatcherTimer();
It is better practice to initialize upon the instance to save overhead, though depending on the scope of use. Given i am not aware of how the rest of your code uses it, i suggest it be rewritten as follows
namespace Timer
{
public partial class MainPage : Page
{
DispatcherTimer mytimer = new DispatcherTimer();
int currentcount = 0;
public MainPage()
{
InitializeComponent();
mytimer.Interval = new TimeSpan(0, 0, 0, 1, 0);
mytimer.Tick += new EventHandler(mytime_Tick);
}
private void mytime_Tick(object sender, EventArgs e)
{
timedisplayBlock.Text = (++currentcount).ToString();
}
private void startButton_Click(object sender, RoutedEventArgs e)
{
mytimer.Start();
}
}
}
Saying all of the above, i could not reproduce the fault when replicated...
EDIT - This may be your issue (https://stackoverflow.com/a/16636862/2110465)
You misspelled the event handler name the event handler definition needs to be changing too, it should be:
mytimer.Tick += mytime_Tick; // removed the 'r'
Change
private void mytimer_Tick(object sender, EventArgs e)
to
private void mytimer_Tick(object sender, object e).
I am trying to make a program with 2 timers in it running at different intervals. Currently I have 1 timer working fine and I need to have another one running. My code for the first timer that works looks like this:
private void startButton_Click(object sender, RoutedEventArgs e)
{
Random rand = new Random();
int ranMin = rand.Next(1,24);
int ranSec = rand.Next(0, 59);
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, ranMin, ranSec);
dispatcherTimer.Start();
min.Content = ranMin;
sec.Content = ranSec;
openP();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
**code for timer in here
}
This works fine, but now I need another timer running at a 1second interval with different code and when I try to duplicate this by just making all the dispatcherTimer into dispatcherTimer2 I am running into errors.
I am not sure what you were doing (you should post the error), but the following works with your supplied code:
private void startButton_Click(object sender, RoutedEventArgs e)
{
Random rand = new Random();
int ranMin = rand.Next(1,24);
int ranSec = rand.Next(0, 59);
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, ranMin, ranSec);
dispatcherTimer.Start();
// New timer
System.Windows.Threading.DispatcherTimer dispatcherTimer2 = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer2.Tick += new EventHandler(dispatcherTimer2_Tick);
dispatcherTimer2.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer2.Start();
min.Content = ranMin;
sec.Content = ranSec;
openP();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
//code for timer in here
}
private void dispatcherTimer2_Tick(object sender, EventArgs e)
{
//code for timer2 in here
}
Please, help me to understand how I could stop attempts of executing MethodOne() inside a dispatcherTimer.Tick event handler of WPF DispatcherTimer after first unsuccessful attempt of doing it.
TimeSpan ts = new TimeSpan(0, 0, 5);
DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = ts;
dispatcherTimer.Start();
...
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
try
{
MethodOne()
}
catch (Exception)
{
// Here I would like prevent code from trying to execute MethodOne()
}
}
I would like to set some lock or to stop timer, but trying to do it I faced problems of visibility of other code from inside a Try-Catch construction and not sure how to overcome it correctly.
That's what the "sender" argument is for:
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
try
{
MethodOne()
}
catch (Exception)
{
(sender as DispatcherTimer).Stop();
}
}