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();
}
Related
I have a code like this in windows phone
private void btnrun_Click(object sender, RoutedEventArgs e)
{
t = 0;
Start_timer();
}
public void Start_timer()
{
timer2 = new DispatcherTimer();
timer2.Tick += timer_Tick2;
timer2.Interval = new TimeSpan(0, 0, 0, 0, 1000);
timer2.Start();
}
void timer_Tick2(object sender, object e)
{
t++;
txttime.Text = t.ToString();
}
At first use, the display right from 1,2,3,4 ... but when you click again, it starts from 2, 4, 6 .... click again, the display 3, 6 ...
Is there a way for it to run your display when clicking the button 1,2,3,4 each failure?
thank!!!
Try something like this:
private void btnrun_Click(object sender, RoutedEventArgs e)
{
t = 0;
Start_timer();
}
public void Start_timer()
{
if (timer2 != null)
{
timer2 -= timer_Tick2; // unassigns the event handler
timer2.Stop(); // stops the timer
}
timer2 = new DispatcherTimer();
timer2.Tick += timer_Tick2;
timer2.Interval = new TimeSpan(0, 0, 0, 0, 1000);
timer2.Start();
}
void timer_Tick2(object sender, object e)
{
t++;
txttime.Text = t.ToString();
}
EDIT: If you just want to start the timer once, it could be reduced to:
private void btnrun_Click(object sender, RoutedEventArgs e)
{
t = 0;
if (timer2 == null)
Start_timer();
}
public void Start_timer()
{
timer2 = new DispatcherTimer();
timer2.Tick += timer_Tick2;
timer2.Interval = new TimeSpan(0, 0, 0, 0, 1000);
timer2.Start();
}
void timer_Tick2(object sender, object e)
{
t++;
txttime.Text = t.ToString();
}
Use Stop() method to stop the timer every click before run it again.
private void btnrun_Click(object sender, RoutedEventArgs e)
{
t = 0;
timer2.Stop();
Start_timer();
}
Each timer registers on the same Tick callback:
timer2.Tick += timer_Tick2;
so it's the reason of the problem.
I want to run timer countdown (DispatchTimer) multiple times in a loop, one after another. I want to wait until one timer stops and then run another one.
I'm gonna try something like this:
public TimeSpan CurrentTimeSpan;
private void RunInterval()
{
for (int i = 0; i < 10; i++)
{
RunTimer(new TimeSpan(0, 0, 0, 30));
}
}
private void RunTimer(TimeSpan Timer)
{
DispatcherTimer timer1 = new DispatcherTimer();
timer1.Interval = new TimeSpan(0, 0, 0, 1);
CurrentTimeSpan = Timer;
timer1.Tick += Timer1OnTick;
}
private void Timer1OnTick(object sender, object o)
{
DispatcherTimer timer = (DispatcherTimer) sender;
CurrentTimeSpan = CurrentTimeSpan.Subtract(timer.Interval);
if (CurrentTimeSpan.TotalSeconds == 0)
{
timer.Stop();
}
}
My problem is that method RunInterval or RunTimer doesn't wait until timer will stop.
What can I do about it?
Maybe something like this?
private int hitCount = 0;
public TimeSpan CurrentTimeSpan;
private void RunInterval()
{
RunTimer(new TimeSpan(0, 0, 0, 30));
}
private void RunTimer(TimeSpan Timer)
{
DispatcherTimer timer1 = new DispatcherTimer();
timer1.Interval = new TimeSpan(0, 0, 0, 1);
CurrentTimeSpan = Timer;
timer1.Tick += Timer1OnTick;
}
private void Timer1OnTick(object sender, object o)
{
DispatcherTimer timer = (DispatcherTimer)sender;
if(hitCount == 10)
{
timer.Tick -= Timer1OnTick
}
CurrentTimeSpan = CurrentTimeSpan.Subtract(timer.Interval);
if (CurrentTimeSpan.TotalSeconds == 0)
{
timer.Stop();
}
}
Instead of looping to create the timers just keep track in the event how many times it's been hit and then unhook the event once that target has been achieved.
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.
}
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
}
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();
}