Threaded form controlling

  • Thread starter Thread starter BveB
  • Start date Start date
B

BveB

I want to make a listener thread for some actions on windows forms but
when I use Thread class cpu usage goes to 100% what can I do?

private void Th_Func(){
while(true){
if(STATE_FORMCLICK){
//do some work
}
if(STATE_RECBUTTONCLICK){
//do some work
}
}
}

Wht is wrong?
 
BveB said:
I want to make a listener thread for some actions on windows forms but
when I use Thread class cpu usage goes to 100% what can I do?

private void Th_Func(){
while(true){
if(STATE_FORMCLICK){
//do some work
}
if(STATE_RECBUTTONCLICK){
//do some work
}
}
}

Wht is wrong?

What's wrong is you're tight-looping. You should use something like
Monitor.Wait/Pulse or a Manual or AutoResetEvent so that your thread
spends most of its time waiting, instead of it polling the whole time.
 
Hi,

Jon Skeet said:
What's wrong is you're tight-looping. You should use something like
Monitor.Wait/Pulse or a Manual or AutoResetEvent so that your thread
spends most of its time waiting, instead of it polling the whole time.

And, if he doesn't have anything to wait for, he could use a timer instead
of looping like that.

Cheers,
 
BveB said:
please give an example

I don't have one to hand, but read any tutorial on AutoResetEvent or
ManualResetEvent or Monitor.Wait/Pulse and you should find what you're
after.
 
Back
Top