Visual Studio 2015 c# Timer problems - c#

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

Related

Timer doesn't raise during long event

I am trying to present to operator timer 00:00 on the start button event ( that start my program - long time event ) I enabled the timer but it doesn't tick only when the event finished or if message box occur .
I am using C# form timer .
Any idea how to implement in C# timer during long event ? I think is related to threading timer ? Any Example ? Please help ! thanks
private void Timer_elapsed_Tick(object sender, EventArgs e)
{
timeSec++;
if(timeSec >= 60)
{
timeSec = 0;
timeMin++;
}
DrawTime();
}
private void DrawTime()
{
lbSec.Text = string.Format("{0:00}", timeSec);
lbMin.Text = string.Format("{0:00}", timeMin);
}
private void start_Click(object sender, EventArgs e)
{
timer_elapsed.Enabled = true;
//do some event (long time)
//timer not works during this event
}
// when the event finished timer works
I think timer control not associated with Tick event. check your your code form designer cs whether you configured wired events handlers correctly for "Timer_elapsed_Tick". this.timer_elapsed.Tick += new System.EventHandler(this.Timer_elapsed_Tick);
solutions:
1.To works with System.Timers.Timer and not the windows form timer .
2.Add on click event Application.DoEvents .

Winform application Timer doesn´t work correctly

i´m doing a winform app, and the timer isn´t working fine. First time works, and then, it doesn´t.
Here is the code:
public void GetNewTurn(Turn turn)
{
_tmrStarTime = DateTime.Now;
timer1.Start();
timer1.Tick += tmr1_Tick;
}
private void tmr1_Tick(object sender, EventArgs e)
{
//timer code here
timer.stop();
}
So, the idea is:
GetNewTurn is a function which is invoked from another place. The first time which I invoke it, works fine, then doesn´t. I put a breakpoint inside the tmr1_Tick, and i can see that it just works the first time, then, doesn´t.
In the Timer properties, i set Enable = True.
What i´m doing wrong?
Thanks!
You shoudn't stop the tmr1_Tick in the first tick
public void GetNewTurn(Turn turn)
{
_tmrStarTime = DateTime.Now;
timer1.Start();
timer1.Tick += tmr1_Tick;
}
private void tmr1_Tick(object sender, EventArgs e)
{
//the code for each tick
}
Only add the handler once in the constructor or OnLoad override.
timer1.Tick += tmr1_Tick;
public void GetNewTurn(Turn turn)
{
_tmrStarTime = DateTime.Now;
timer1.Start();
}

C# get current system time

So I have a program that I need to be able to get the current time. This is a winform and I have a timer that starts up when the winform is loaded and the interval is 1000. Every tick it checks the time and sets a label on the winform to display that time. I use DateTime.Now.Hour; to determine the hour (which is just what I need). My problem is even though this code is on a timer it only displays the time that was when the winform started and doesnt update it. What do I do to get the current and updating hour of the day?
EDIT:
Here is the code
//code for hour variable
private int time = DateTime.Now.Hour;
//Code for timer
private void mainTimer_Tick(object sender, EventArgs e)
{
//code run every time mainTimer gets a tick
label1.Text = time.ToString();
}
Your class field won't be re-evaluated again until you set it explicitly.
This is how you should do it:
label1.Text = DateTime.Now.Hour.ToString();
private void Clock(object sender, EventArgs e)
{
DateTime dtn = DateTime.Now.Hour;
label1.Text = dtn.ToString();
}
then put timer tick on it

how to increment the variable correctly?

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.

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

Categories