Multiple timers in same program - c#

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
}

Related

Change messageTimer.Interval at runtime even if the messageTimer is in another method

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();
}
));

Why MediaElement doesnot work on call-time?

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();
}

Using a timer to stop another timer

I am an amateur for C# and I am doing a project.
I have used 3 timers where one is a 20 seconds round timer.
I want that after 20 seconds have passed, the other 2 timers must stop
I am not able to access/call the other 2 timers from the round timer.
Here is the code:
public void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DispatcherTimer tgtTimer = new DispatcherTimer();
tgtTimer.Tick += new EventHandler(tgtTimer_tick);
tgtTimer.Interval = new TimeSpan(0, 0, 3);
tgtTimer.Start();
DispatcherTimer txbTimer2 = new DispatcherTimer();
txbTimer2.Tick += new EventHandler(txbTimer2_tick);
txbTimer2.Interval = new TimeSpan(0, 0, 0, 4, 000);
txbTimer2.Start();
DispatcherTimer rt = new DispatcherTimer();
rt.Tick += new EventHandler(rt_tick);
rt.Interval = new TimeSpan(0, 0, 1);
rt.Start();
}
private void tgtTimer_tick(object sender, EventArgs e)
{ //some code }
private void txbTimer2_tick(object sender, EventArgs e)
{ //some code }
int i = 20 //for the 20 sec round timer
private void rt_tick(object sender, EventArgs e) //round timer
{
if(i!=0)
{
i--;
txbTime.Text = "";
txbTime.Text = Convert.ToString(i) + "s";
}
else
{
this.Opacity = 0.3;
//tgtTimer and txbTimer2 can't be accessed from here
}
}
How should I do stop the 2 timers?
You should try to add the timers outside of the mouseleftbutton down event like this:
DispatcherTimer tgtTimer = new DispatcherTimer();
DispatcherTimer txbTimer2 = new DispatcherTimer();
DispatcherTimer rt = new DispatcherTimer();
public void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
tgtTimer.Tick += new EventHandler(tgtTimer_tick);
tgtTimer.Interval = new TimeSpan(0, 0, 3);
tgtTimer.Start();
txbTimer2.Tick += new EventHandler(txbTimer2_tick);
txbTimer2.Interval = new TimeSpan(0, 0, 0, 4, 000);
txbTimer2.Start();
rt.Tick += new EventHandler(rt_tick);
rt.Interval = new TimeSpan(0, 0, 1);
rt.Start();
}
Either move the references to your timers out of the button click:
DispatcherTimer tgtTimer = new DispatcherTimer();
DispatcherTimer tgtTimer2 = new DispatcherTimer();
public void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DispatcherTimer tgtTimer = new DispatcherTimer(); //here
tgtTimer.Tick += new EventHandler(tgtTimer_tick);
tgtTimer.Interval = new TimeSpan(0, 0, 3);
tgtTimer.Start();
DispatcherTimer txbTimer2 = new DispatcherTimer(); //and here
txbTimer2.Tick += new EventHandler(txbTimer2_tick);
txbTimer2.Interval = new TimeSpan(0, 0, 0, 4, 000);
txbTimer2.Start();
//or inline the method to stay within scope
DispatcherTimer rt = new DispatcherTimer();
rt.Tick += (ob, ev) =>
{
//still in scope
txbTimer2.Start();
};
rt.Interval = new TimeSpan(0, 0, 1);
rt.Start();
}

DispatcherTimer to run randomly in WPF

I need to do some stuff in my application while application is running. I put it in dispatcher to work withing given time intervals. But now I want to randomized things.
I want to set random time intervals.
dTSettings.Tick += new EventHandler(dTSettings_Tick);
dTSettings.Interval = new TimeSpan(0, SetRandomTimer(), 0);
dTSettings.Start();
how to do that?
private DispatcherTimer _timer = new DispatcherTimer();
private Random rand = new Random();
public void InitAndStartTimer()
{
_timer.Tick += dispatcherTimer_Tick;
_timer.Interval = TimeSpan.FromSeconds(rand.Next(1, 10)); // From 1 s to 10 s
_timer.Start();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
_timer.Interval = TimeSpan.FromSeconds(rand.Next(1, 10)); // From 1 s to 10 s
// Do your work.
}

pausing a video after every 10s in c#

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();
}

Categories