how to increment the variable correctly? - c#

when I start the timer ..timel is incremented normally ..but as soon as I stop the timer i.e. call the click_TimerStop function and start the timer again...the timel variable is incremented by timel+=2..and when I repeat the process ..it is increased by timel+=3..and it goes on and on ...how do I correct this ?..
DispatcherTimer clktimer = new DispatcherTimer();
private void click_TimerStart(object sender, RoutedEventArgs e)
{
clktimer.Start();
clktimer.Interval =new TimeSpan(0,0,1);
clktimer.Tick +=clktimer_tick;
}
private int timel = 0;
private void clktimer_tick(object sender, object e)
{
timel++;
timerSecond.Text = timel.ToString();
}
private void click_TimerStop(object sender, RoutedEventArgs e)
{
clktimer.Stop();
}

add
clktimer.Tick -=clktimer_tick;
before
clktimer.Tick +=clktimer_tick;
you'll unsubscribe and subscribe to event, so only one handler will be active at a time
and It's better to call start() after you set all settings to timer

It's because you're continually adding the clktimer_tick event handler each time you start the timer. Initialize your timer somewhere where it will only be called once and not every time you start, because there's no need to keep setting the same settings each time.

Related

button click every interval c#

I have a button where I want to it to click every 5 seconds.
I also have another button where the first button will stop clicking.
This is the code so far I have
private void StartButton_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
Timer timer = new Timer { Interval = 5000 };
timer.Start();
}
How do I go on about achieving this?
if the button will set timer start/stop called 'ControlButton', and another one called 'DoJobButton'. Setup a timer interval = 5000 like your code. and in's tick event have this code only
DoJobButton.PerformClick();
this will trigger DoJobButton button's click action.
and set timer enable/disable in ControlButton's click event.
I think this is enough for your goal.

Visual Studio 2015 c# Timer problems

Hey just wondering if anyone could help me out with a problem I'm encountering with a timer in my windows form application. Here is the code I'm using:
private void game_Timer_Tick(object sender, EventArgs e)
{
while (true)
{
int count = 0;
count++;
timeLabel.Text = TimeSpan.FromSeconds(count).ToString();
}
}
The problem I'm having is that whenever the window that this applies to opens and then after that nothing happens and I'm unable to do anything. When removing this code the window works fine so I'm unsure why its not working in relation to this section of code. Any thoughts? Thanks
If you want to display number of seconds since timer start, then declare field for holding start time:
private DateTime startTime;
Assign this field when you are starting timer:
game_Timer.Interval = 1000; // fire event each second
startTime = DateTime.Now;
game_Timer.Start();
And use it in Tick handler:
private void game_Timer_Tick(object sender, EventArgs e)
{
timeLabel.Text = (DateTime.Now - startTime).ToString();
}
What is wrong with your code? You have infinite loop inside Tick event handler. So when event fires first time, you are entering this loop and never exit it. And you are unable to do anything, because your application is busy with constant updating time label.
You can also use counter instead of saving timer start time. But you will need field anyway:
private int count = 0;
And event handler:
private void game_Timer_Tick(object sender, EventArgs e)
{
timeLabel.Text = TimeSpan.FromSeconds(++count).ToString();
}

building a stop watch with help of timer

I want to add functionality to my WinForms so that when it starts a counter starts which will be in hh:mm. I know this can be done using a timer. I have made a time label which displays the current time, but I don't know how to start the timer when the form is loaded. Is there any method or class for that?
Place Timer component to your form (drag it from ToolBox - it's imporant, because timer should be registered as form's component to be disposed correctly when form closes). Set timer's Interval property to 60000 (that's equal to one minute). And subscribe to Tick event:
void timer1_Tick(object sender, EventArgs e)
{
if (endTime < DateTime.Now)
{
MessageBox.Show("Time is out!");
timer1.Stop();
return;
}
timeLabel.Text = (endTime - DateTime.Now).ToString(#"hh\:mm");
}
On Form_Load event handler start timer and save countdown end time:
private DateTime endTime; // field to store end time
void Form1_Load(object sender, EventArgs e)
{
endTime = DateTime.Now.AddMinutes(120); // set countdown to 120 minutes
timer1.Start();
}
The creation of a timer is very simple and straight forward:
Timer t1 = new Timer();
t1.Interval = 100;
t1.Tick+=new EventHandler(t1_Tick);
t1.Start();
void t1_Tick(object sender, EventArgs e)
{
}
For more information see http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.80).aspx

How to let System.Windows.Forms.Timer to run the Tick handler immediately when starting?

The Timer following is System.Windows.Forms.Timer
C# Code:
Timer myTimer = new Timer();
myTimer.Interval = 10000;
myTimer.Tick += new EventHandler(doSth);
myTimer.Start();
The timer will doSth every 10 seconds, but how to let it doSth immediately when starting?
I tried to create a custom timer which extends Timer, but I can't override the Start method.
For now, my code is:
myTimer.Start();
doSth(this, null);
I don't think it's good. How to improve it?
It's perfect. Don't change a thing.
I'm not sure of your exact requirements but why not put the code inside your timer callback inside another method?
e.g.
private void tmrOneSec_Tick(object sender, System.EventArgs e)
{
DoTimerStuff();
}
private void DoTimerStuff()
{
//put the code that was in your timer callback here
}
So that way you can just call DoTimerStuff() when your application starts up.
The timer has to have form level scope, and it's not clear that you have that. I whipped up a small example out of curiosity and it is working for me:
private void Form1_Load(object sender, EventArgs e)
{
txtLookup.Text = "test";
DoSomething();
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
DoSomething();
}
private void DoSomething()
{
txtLookup.Text += "ticking";
}
Every 10 seconds it appends another "ticking" to the text in the textbox.
You say,
"The timer will doSth every 10 seconds, but how to let it doSth immediately when starting?"
I say, call doSth immediately before calling the timer's start method

Timers in C#, how to control whats being sent to the timer1_tick

In this following function, that gets executed whenever I do
timer1.Enabled = true
private void timer1_Tick(object sender, EventArgs e)
{
//code here
}
How can I control what gets send to the (object sender, EventArgs e) ?
I want to use its parameters
The method signature is fixed, so you can't pass extra parameters to it. However, the this reference is valid within the event handler, so you can access instance members of the class (variables declared inside class but outside of any method).
1) You can use Tag property of your timer as userState
void timer1_Tick(object sender, EventArgs e)
{
Timer timer = (Timer)sender;
MyState state = timer.Tag as MyState;
int x = state.Value;
}
2) You can use field of reference type to read it in Timer's thread
void timer1_Tick(object sender, EventArgs e)
{
int x = _myState.Value;
}
3) You can use System.Threading.Timer to pass state to timer event handler
Timer timer = new Timer(Callback, state, 0, 1000);
If you want to access Timer's property in the timer1_tick method, you could do via
this.timer1 ex: this.timer1.Enabled =false;
or
Timer timer = (Timer) sender;
timer.Enabled = false;
Maybe you could make an inheritance from timer class, and there, cast the tick event (from Timer) into a tick_user event or something like this that modify de params and put into EventArgs (this is the right place to do, not in sender) other parameters you want. Also you can make a method with more or less parameters, it's up to you.
Hope this helps.

Categories