A timer & a while function
-
I am having a problem with a timer in my program. I am sure I am missing serveral things that someone else will see easly. this is a short example of my code.
private void button_click_1(object sender, eventargs e) { starttimer(); Dothewhile(); } public void starttimer() { timer1.start(); timer1.interval = 1000; } private void timer1_Tick(object sender, EventArgs e) { MessageBox.Show("hello"); } public void Dothewhile() { while (Run == true) { // do other stuff and when it detects its done it makes Run == false } }
I am assuming the while statment is over taking the timer event and i just cant see when the timer hits but dont know for sure because i am stupid. But i would like to have some input on the ideal why the timer is never seen unless i turn the while statement off. thanks danzar Superman was my hero till he got a real job. -
I am having a problem with a timer in my program. I am sure I am missing serveral things that someone else will see easly. this is a short example of my code.
private void button_click_1(object sender, eventargs e) { starttimer(); Dothewhile(); } public void starttimer() { timer1.start(); timer1.interval = 1000; } private void timer1_Tick(object sender, EventArgs e) { MessageBox.Show("hello"); } public void Dothewhile() { while (Run == true) { // do other stuff and when it detects its done it makes Run == false } }
I am assuming the while statment is over taking the timer event and i just cant see when the timer hits but dont know for sure because i am stupid. But i would like to have some input on the ideal why the timer is never seen unless i turn the while statement off. thanks danzar Superman was my hero till he got a real job.You are using a System.Windows.Forms timer, which ALWAYS runs on the main GUI thread. Therefore, your timer1_Tick method is waiting for the main thread to become available, which only happens when you exit the while loop. If you want to receive the Tick event in a separate thread (which you indicate is the desire from your post), then use either System.Timers.Timer or System.Threading.Timer, or start your method DoTheWhile in a new thread, which will free your main GUI thread for the timer. Jeff
-
I am having a problem with a timer in my program. I am sure I am missing serveral things that someone else will see easly. this is a short example of my code.
private void button_click_1(object sender, eventargs e) { starttimer(); Dothewhile(); } public void starttimer() { timer1.start(); timer1.interval = 1000; } private void timer1_Tick(object sender, EventArgs e) { MessageBox.Show("hello"); } public void Dothewhile() { while (Run == true) { // do other stuff and when it detects its done it makes Run == false } }
I am assuming the while statment is over taking the timer event and i just cant see when the timer hits but dont know for sure because i am stupid. But i would like to have some input on the ideal why the timer is never seen unless i turn the while statement off. thanks danzar Superman was my hero till he got a real job. -
You are using a System.Windows.Forms timer, which ALWAYS runs on the main GUI thread. Therefore, your timer1_Tick method is waiting for the main thread to become available, which only happens when you exit the while loop. If you want to receive the Tick event in a separate thread (which you indicate is the desire from your post), then use either System.Timers.Timer or System.Threading.Timer, or start your method DoTheWhile in a new thread, which will free your main GUI thread for the timer. Jeff
-
danzar wrote:
I am assuming the while statment is over taking the timer event
#1 Don't make assumptions. #2 Don't make up terminology in text based communications ( over taking the timer ) because your readers won't know what you are talking about.
led mike wrote:
danzar wrote: I am assuming the while statment is over taking the timer event #1 Don't make assumptions.
I know its a bad habbit I have. sorry
led mike wrote:
#2 Don't make up terminology in text based communications ( over taking the timer ) because your readers won't know what you are talking about.
My poor choice of words, was from the lack of not knowing the correct terminolgy for the issuse I was having. I know now that I was wrong in my description of the issuse I was having. Sorry again for the mistake but was the result of learning. A wise man said- You never learn unless you ask for help. :)
-
led mike wrote:
danzar wrote: I am assuming the while statment is over taking the timer event #1 Don't make assumptions.
I know its a bad habbit I have. sorry
led mike wrote:
#2 Don't make up terminology in text based communications ( over taking the timer ) because your readers won't know what you are talking about.
My poor choice of words, was from the lack of not knowing the correct terminolgy for the issuse I was having. I know now that I was wrong in my description of the issuse I was having. Sorry again for the mistake but was the result of learning. A wise man said- You never learn unless you ask for help. :)