enable function at timer stop c# - c#

I guess this question has been asked like a 1000 times but I donĀ“t know what I`m doing wrong. I use a timer in C#. If it expires, a value should be set to 1 in order to enable an other function. My intention is to block a function till the timer ticks. If the timer stops, the new function should be enable.
using System.Timers;
public float testNumber;
private Timer timer;
void Gearup ()
{
if (currentGear < 5) {
currentGear = currentGear + 1;
timer = new Timer ();
timer.Interval=2000;
timer.AutoReset = true;
timer.Start();
testNumber = 10;
}
if (timer.Enabled != false) {
testNumber = 1;
}
Could you guys help me ? Every time I test my code, test number is immediately set to 1. Please be gentle, I know that I
am a noob.
update Question: My problem is, that my game character should shift a gear in the game. Once the gear is shifted, the character shouldnt be allowed to shift it right back. Thats why i want to block the shift function for a coupe of seconds.

So you need to start a timer with an interval of two seconds. When the timer elapses you want to set the variable testNumber to one.
This happens just one time then stops.
Then you should set the AutoReset property to false and add an event handler for the Elapsed event
testNumber = 10;
Timer timer = new Timer();
timer.Elapsed += (s, o) =>
{
Console.WriteLine("Timer elapsed");
testNumber = 1;
};
timer.Interval = 2000;
timer.AutoReset = false;
timer.Start();
Now you don't need to stop and check anything. Continue with your code exiting from the Gearup method. The global variable testNumber will be set to 1 after the Interval period elapses.

When your timer starts timer.Enabled is set to true. And then your programm jumps in the
if (timer.Enabled != false) {
testNumber = 1;
}
block. Because timer.Enabled is not equal false it's true! You have to cahnge it to
if (timer.Enabled == false) {
testNumber = 1;
//for timer restart use:
timer.Start();
}
or
if (timer.Enabled != true) {
testNumber = 1;
//for timer restart use:
timer.Start();
}

Related

How to periodically trigger an time-lapse event every specific time interval

I wanted to create a time-lapse control, for example, when the program starts, after time interval t1, trigger event A (e.g switch on), then wait after time interval t2, trigger event B (e.g.switch off), and this process will be executed periodically for n times.
What I have achieved so far is I could loop the two events only when t1=t2, I don't know how to make t1 and t2 two separate controllable variables.
public partial class Form1 : Form
{
private int counter; //counter to control the cycle times
private int EventIndex;//control the event status
private void InitializeTimer()
{
counter = 0;
EventIndex = 0;
timer1.Enabled = true; // Hook up timer's tick event handler.
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
}
//function for switch the event status
private void UpdateEvent()
{
EventIndex = 1 - EventIndex; // loop event index between 1 and 0
if (EventIndex.Equals(1))
{
//Exceute command for event A
}
else
{
//Exceute command for event A
}
}
private void timer1_Tick(object sender, EventArgs e)
{
decimal t = numericUpDown1.Value; //time interval t between two events
int interval = Convert.ToInt32(t);
decimal n = numericUpDown2.Value; //periodically excute the process n times
int cycle = Convert.ToInt32(n);
//using the numeric input value to set up the interval time
timer1.Interval = interval
if (counter >= cycle)
{
// Exit loop code.
timer1.Enabled = false;
counter = 0;
}
else
{
UpdateEvent();
counter = counter + 1;// Increment counter.
}
}
}
Have UpdateEvent set the timer interval after an event:
private void UpdateEvent()
{
if (EventIndex.Equals(1))
{
timer1.Interval = //delay until event B;
}
else
{
timer1.Interval = //delay until event A;
}
}
I'll suggest you make two timer for each event.
timerA is interval t1, trigger event A.
timerB is interval t2, trigger event B.
By seperating timer, you may not accidentally change your timer interval into unexpected reults.
void UpdateEventA(){
if( counter >= cycle ){
timerA.Enable = false;
return;
}
// A working
timerA.Enable = false;
timerB.Enable = true;
}
void UpdateEventB(){
// B working
timerB.Enable = falsed;
timerA.Enable = true;
counter ++;
}
However if you have Event ABCDE and need to execute in sequence. I'll suggest you use one timer and calculate interval internally in single Tick event.

Using timer in windows form application

I am new to coding and are having trouble.
I want to use a timer that will start upon pressing ScatterMode tab, it will start counting 4sec before running "Dosomething" function. This cycle will repeat itself until i decide to stop the program. But the problem i get is, this code only run correctly for like 2loop, after that the timer sort of go crazy LOL.
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
//ScatterMode Tab
private void Scatter_modeToolStripMenuItem_Click(object sender, EventArgs e)
{
timer.Interval = 4000;
timer.Enabled = true;
timer.Tick += new EventHandler(Dosomething);
timer.Start();
}
private void Dosomething (object sender, EventArgs e)
{
timer.Stop();
timer.Enabled = false;
Grab.buffer(out buffer, out status, 6000);
Scatter_mode(buffer);
pictureBox1.Refresh();
int done_grab = 1;
if (doneGrab == 1)
{
timer.Interval = 4000;
timer.Enabled = true;
timer.Tick += new EventHandler(Scatter_modeToolStripMenuItem_Click);
timer.Start();
done_grab = 0;
}
}
Adding a new event handler to a timer, to handle its tick event, inside the handler for the tick event will indeed cause the timer to go "crazy". Every time the timer raises its event, another event handler (that responds to events raised) will be added. This means the next time the timer ticks, the event code will run twice. Two new event handlers will be added. Next time the timer ticks, the code will run 4 times. 4 event handlers will be added ... and so on
Remove this line from your code:
timer.Tick += new EventHandler(Scatter_modeToolStripMenuItem_Click);
And move this line into your form's constructor:
timer.Tick += new EventHandler(Dosomething);
You only want to wire this event handler up once. Every time the timer's interval elapses, the code will run, once :)
I'll also do a bit of a peer review of your code, see the comments:
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
//ScatterMode Tab
private void Scatter_modeToolStripMenuItem_Click(object sender, EventArgs e)
{
timer.Interval = 4000; //can go in the constructor also; don't need to set repeatedly
timer.Enabled = true;
timer.Tick += new EventHandler(Dosomething); //move to constructor
timer.Start(); //this isn't needed - you already Enabled the timer, which started it
}
private void Dosomething (object sender, EventArgs e)
{
timer.Stop(); //use this
timer.Enabled = false; //or this. It's not required to do both
Grab.buffer(out buffer, out status, 6000); //if these lines crash then your timer will
Scatter_mode(buffer); //only restart if the toolstripmenuitemclick
pictureBox1.Refresh(); //above runs.. is it what you wanted?
int done_grab = 1; //not needed
if (doneGrab == 1) //this will always evaluate to true, it is not needed
{
timer.Interval = 4000; //the interval is already 4000, not needed
timer.Enabled = true; //careful; your timer may stop forever if the code above crashes
timer.Tick += new EventHandler(Scatter_modeToolStripMenuItem_Click); //remove
timer.Start(); //not needed
done_grab = 0; //not needed
}
}

C# Timer inside a property

I'm trying to implement a timer for a checkbox. The binding for the checkbox is done using the CaptureColorBind property. When ever I click the capture color checkbox (captureColor = true), it needs to be checked for 5 seconds and then checkbox needs to be unchecked. I'm trying to print the datetime before and after the timer to verify. Its printing the before time properly, but the datetime I print in the elapsed event handler gets printed n times depending on the number of times I click the capture color checkbox. That is the first time I click, it prints the date and time once, the second time I click, it prints twice and so on. Not sure what I'm doing wrong.
private System.Timers.Timer timer = new System.Timers.Timer();
public bool CaptureColorBind
{
get
{
return this.captureColor;
}
set
{
this.captureColor = value;
if (captureColor == true)
{
Console.WriteLine(DateTime.Now.ToString());
Console.WriteLine();
timer.Elapsed += new ElapsedEventHandler(capturecolor_timer);
timer.Interval = 5000;
timer.Enabled = true;
}
if (null != this.PropertyChanged)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("CaptureColorBind"));
}
}
}
// Timer for capturecolor checkbox
private void capturecolor_timer(object sender, ElapsedEventArgs e)
{
timer.Enabled = false;
this.captureColor = false;
//this.colorCheckbox.IsChecked = false;
Console.WriteLine(DateTime.Now.ToString());
Console.WriteLine();
if (null != this.PropertyChanged)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("CaptureColorBind"));
}
}
You are adding a new event handler every time the value is set. You should only add it once.
Try adding the event handler in the object's constructor so it only gets set once, and just re-enabling the timer when the property is set.
private System.Timers.Timer timer = new System.Timers.Timer();
public MyObject()
{
timer.Elapsed += new ElapsedEventHandler(capturecolor_timer);
timer.Interval = 5000;
}

C# timer stop after some number of ticks automatically

How to stop a timer after some numbers of ticks or after, let's say, 3-4 seconds?
So I start a timer and I want after 10 ticks or after 2-3 seconds to stop automatically.
Thanks!
You can keep a counter like
int counter = 0;
then in every tick you increment it. After your limit you can stop timer then. Do this in your tick event
counter++;
if(counter ==10) //or whatever your limit is
yourtimer.Stop();
When the timer's specified interval is reached (after 3 seconds), timer1_Tick() event handler will be called and you could stop the timer within the event handler.
Timer timer1 = new Timer();
timer1.Interval = 3000;
timer1.Enabled = true;
timer1.Tick += new System.EventHandler(timer1_Tick);
void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop(); // or timer1.Enabled = false;
}
i generally talking because you didn't mention which timer, but they all have ticks... so:
you'll need a counter in the class like
int count;
which you'll initialize in the start of your timer, and you'll need a dateTime like
DateTime start;
which you'll initialize in the start of your timer:
start = DateTime.Now;
and in your tick method you'll do:
if(count++ == 10 || (DateTime.Now - start).TotalSeconds > 2)
timer.stop()
here is a full example
public partial class meClass : Form
{
private System.Windows.Forms.Timer t;
private int count;
private DateTime start;
public meClass()
{
t = new Timer();
t.Interval = 50;
t.Tick += new EventHandler(t_Tick);
count = 0;
start = DateTime.Now;
t.Start();
}
void t_Tick(object sender, EventArgs e)
{
if (count++ >= 10 || (DateTime.Now - start).TotalSeconds > 10)
{
t.Stop();
}
// do your stuff
}
}
Assuming you are using the System.Windows.Forms.Tick. You can keep track of a counter, and the time it lives like so. Its a nice way to use the Tag property of a timer.
This makes it reusable for other timers and keeps your code generic, instead of using a globally defined int counter for each timer.
this code is quiet generic as you can assign this event handler to manage the time it lives, and another event handler to handle the specific actions the timer was created for.
System.Windows.Forms.Timer ExampleTimer = new System.Windows.Forms.Timer();
ExampleTimer.Tag = new CustomTimerStruct
{
Counter = 0,
StartDateTime = DateTime.Now,
MaximumSecondsToLive = 10,
MaximumTicksToLive = 4
};
//Note the order of assigning the handlers. As this is the order they are executed.
ExampleTimer.Tick += Generic_Tick;
ExampleTimer.Tick += Work_Tick;
ExampleTimer.Interval = 1;
ExampleTimer.Start();
public struct CustomTimerStruct
{
public uint Counter;
public DateTime StartDateTime;
public uint MaximumSecondsToLive;
public uint MaximumTicksToLive;
}
void Generic_Tick(object sender, EventArgs e)
{
System.Windows.Forms.Timer thisTimer = sender as System.Windows.Forms.Timer;
CustomTimerStruct TimerInfo = (CustomTimerStruct)thisTimer.Tag;
TimerInfo.Counter++;
//Stop the timer based on its number of ticks
if (TimerInfo.Counter > TimerInfo.MaximumTicksToLive) thisTimer.Stop();
//Stops the timer based on the time its alive
if (DateTime.Now.Subtract(TimerInfo.StartDateTime).TotalSeconds > TimerInfo.MaximumSecondsToLive) thisTimer.Stop();
}
void Work_Tick(object sender, EventArgs e)
{
//Do work specifically for this timer
}
When initializing your timer set a tag value to 0 (zero).
tmrAutoStop.Tag = 0;
Then, with every tick add one...
tmrAutoStop.Tag = int.Parse(tmrAutoStop.Tag.ToString()) + 1;
and check if it reached your desired number:
if (int.Parse(tmrAutoStop.Tag.ToString()) >= 10)
{
//do timer cleanup
}
Use this same technique to alternate the timer associated event:
if (int.Parse(tmrAutoStop.Tag.ToString()) % 2 == 0)
{
//do something...
}
else
{
//do something else...
}
To check elapsed time (in seconds):
int m = int.Parse(tmrAutoStop.Tag.ToString()) * (1000 / tmrAutoStop.Interval);

Save remaining seconds in countdown timer

I have a problem. I want to save the remaining seconds in countdown timer (for example, the remaining time = 12 seconds) i want to save that 12 seconds in a variable.
this is my code
int order = 0;
bool right = true;
DispatcherTimer timer1 = new DispatcherTimer();
private void timer_start()
{
timer1.Interval = new TimeSpan(0, 0, 0, 1);
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
int remainingSecond;
int tik = 15;
void timer1_Tick(object sender, EventArgs e)
{
this.Timer.Text = tik.ToString();
if (tik > 0)
{
tik--;
if (this.order >= 5)
{
timer1.Stop();
if (right)
{
remainingSecond = tik;
}
else
remainingSecond = 0;
}
}
else
{
remainingSecond = 0;
timer1.Stop();
}
}
everytime I write "remainingSecond" , its value is always 0. I wish that remainingSecond value is 12. Help me, please. Thanks
You assigned order = 0 but did not increase it any where and set this condition
if (this.order >= 5) which will never true. So it will keep decrementing your tik and at the end your this condition if (tik > 0) will become false. So else will be executed and it will set your remainingSecond to ZERO. Thats why you are getting ZERO as output.
Your timer is ticking in every 1milisecond. The timer will start, and it will tick immediately, at that time order will be zero, and your else statement will get executed that will set remainingSeconds to ZERo and will Stop the timer as well. So the clicking on the buttons won't do anything for you.
Try to set the interval for 1 second instead of 1 millisecond

Categories

Resources