|
In my application I need to use a timer. Depending on it the text of a Label control must be changed and when the time counter reaches the value of '0' an event must happen.
Now so far I wasn't even able to change the value of Label Text property.
I can't figure out what is wrong. Samples I tried from books and MSDN worked just fine, but not the timer in my application. All I get is an infinite 'while' loop. This is the code I used:
this.countDownTimer.Enabled = true;
while(this.timeCounter > 0)
{
this.countDownTimer.Tick += new EventHandler(this.countDownTimer_Tick);
}
this.countDownTimer.Enabled = false;
private void countDownTimer_Tick(object sender, EventArgs e)
{
this.timeCounter--;
this.lblTimeLeft.Text = timeCounter.ToString();
}
Would someone be so kind to help me with this. I have wasted a full day trying to make it work but no luck so far. Thank you!
|
|
|
|
|
vitaco wrote: while(this.timeCounter > 0){ this.countDownTimer.Tick += new EventHandler(this.countDownTimer_Tick); }
If timerCounter > 0, then your loop will run forever, as nothing is going to decrement it. also, your tick event is going to have many, many events tied to it ( that function will be called over and over ).
I think you're confusing timers and threads. Your timer will try to run, but it won't be able to, because your while loop will be eating the processor time.
Also, only set the tick event once, setting it more than once causes the function to be called over and over. Just remove the while loop, and start the timer ( you don't even to that, as far as I can see ), and it should work fine.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Well, I removed the 'while' loop. When I run the Debug mode I can see (as previously) that the line "this.countDownTimer.Tick += new EventHandler(this.countDownTimer_Tick)" never leads to execution of "countDownTimer_Tick". The lines of code
timeCounter--;
this.lblTimeLeft.Text = timeCounter.ToString();
are never accessed.
I'm certainly missing something. Can you help me with the actual line/lines of code that should do the trick?
|
|
|
|
|
in your first post u are disabling the timer after registering. remove this line
this.countDownTimer.Enabled = false;
and add it after
this.lblTimeLeft.Text = timeCounter.ToString();
Regards
Shajeel
|
|
|
|
|
As I said, the Timer has a Start method, I believe. Or, it's Enabled property has to be 'true'.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
I've tried both. But it just doesn't work.
If I get it right when the program gets to the line
this.countDown.Start();
the 'countDown_Tick' must be executed. In my case it does not. For some
reason the program just 'remarks' the line and executes it after all the rest of the code is passed. I will illustrate the way it goes when I run it in the Debug mode.
//Step 1. The program enters the 'if' body
if(useTimeLimit)
{
this.pnlTimer.Enabled = true;
this.lblTimeLeft.Visible = true;
timeCounter = TimeLimit;
this.countDownTimer.Start();
and then goes further without going to 'countDown_Tick'
}
//Step 3. It goes through the rest of code in the method
else
{
some other code
}
some other code;
some other code;
//Step 4. After it goes through all the code in the method (Step 3)
the program executes the 'countDown_Tick'
private void countDownTimer_Tick(object sender, EventArgs e)
{
while (timeCounter > 0)
{
timeCounter--;
this.lblTimeLeft.Text = timeCounter.ToString();
}
this.countDownTimer.Stop();
}
I know this is stupid, but imagine how stupid I feel in such a situation! Please help!
|
|
|
|
|
vitaco wrote: I've tried both. But it just doesn't work.
If I get it right when the program gets to the line
this.countDown.Start();
the 'countDown_Tick' must be executed.
No i think you misunderstood timer functionality. After Start() function timer interval is started and once interval is elapsed countDownTimer_Tick is called. It is not a function, why do you want it to act like a function. If you want to execute a function then simply call it yourself.
Regards
Shajeel
|
|
|
|
|
create simple window application with form, button, label and timer. In button click and timer tick add following code.
private void button1_Click(object sender, System.EventArgs e)
{
this.timer1.Tick += new EventHandler(timer1_Tick);
this.timer1.Interval = 1000;
this.timer1.Start();
}
private int currentSecond = 0;
private void timer1_Tick(object sender, EventArgs e)
{
this.label1.Text = currentSecond++.ToString();
}
Hope this help
Regards
Shajeel
|
|
|
|
|
Yes it helped! Thank you! Now I will have to get the idea!
Christian Graus also helped me to understand something, namely "A timer will never tick until the method that called it ends". I didn't know that! Probably I will have to call a new method inside the while loop to change the Text property of the Label. Thank you both!
|
|
|
|
|
A timer will never tick until the method that called it ends, the tick is another function call that gets added to the list of pending events. It's not a direct function call, even if the time interval is 0. Timers are not that precise.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Really depends on what timer you are using. A UI timer obviously won't fire until the current queue of messages is empty, but System.Threading.Timer and System.Timers.Timer can both potentially fire even before the method that started them ends.
|
|
|
|
|
I want to develop a ActiveX control in c#.Net, that can handle mouse events, like mouse down, enter, leave, drag and drop events. plz guide me, I will be thank you..
|
|
|
|
|
Why do you want to use ActiveX ?
What info did you find on the web ? What further info do you need ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Thanks for u r reply, actually I want to draw almost 1 to 2 million rectangles (nodes) on the screen, for this purpose I have checked many controls but they don’t support such ability. Now I have decided to use C# drawing library. For this purpose I need a light weight object that can be drawn to the screen and manage mouse handling as well. I think ActiveX can help me in this regards. Plz guide me or if u knows any batter solution plz let me know. Thanks a lot.
|
|
|
|
|
ActiveX is a worthless layer of complexity. 1-2 million nodes is a lot, you will have to be careful to handle it correctly. How do you expect to display that many nodes ? Will your control be scrollable ?
Just writing a C# control will give you as much power as C# is going to give you, without the hassle of a ( potentially useless ) ActiveX layer.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Actually I want to give a full picture of entire business at one quick look. So I have to draw the entire nodes. And yea, my control will be scrollable. If you have any batter idea to perform such a scenario plz let me know. In my point of view I should go towards the c# library and handle all the mouse events by myself. What you suggest.
Thank you.
|
|
|
|
|
faheem424 wrote: In my point of view I should go towards the c# library and handle all the mouse events by myself. What you suggest.
Thank you.
Yes, that seems reasonable. The only thing that you don't need, is ActiveX.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
code for creating toolbox
|
|
|
|
|
IS this a question ? What do you mean by a toolbox ? What have you done already ? Where are you stuck ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
hi!
i m developing EMail client in C# asp.net web application using kerio mail server.
I have sent and received emails using SMTP and POP3 dlls.But i dont know how to add new user of particular domain on my server using c#.
Plz reply soon
thanx
|
|
|
|
|
Please stop cross posting.
---
single minded; short sighted; long gone;
|
|
|
|
|
Is it possible to create new Visualization for Windows Media Player (or winamp) using C#?
Do you know of any article or tutorials? Thanks a lot in advance.
|
|
|
|
|
Hi, I'm wondering how I can ensure a cursor(I'm referring to the IBeam cursor) doesn't appear in a textbox. What I want to do is use a textbox to display some message but I dont want any cursors showing up in it.
Thanks
|
|
|
|
|
if the requirement is such that the user need not enter any data in the textbox, then make the enable property of the textbox to false.
Gautham
|
|
|
|
|
The other reply is on track, but better make the control read only (the ReadOnly property). The user will be able to scroll and the text will appear black (instead of gray), but it will not be editable.
|
|
|
|