Link to home
Create AccountLog in
Avatar of jackjohnson44
jackjohnson44

asked on

c# better sleep

I wrote a program that does something every 10 seconds.  I am using a sleep, but the program seems to lock up duding the sleep.  Is there a better way to do this so the program is not locked up and will respond to a button press?  Basically I start the process wiht one button and it loops 1000 times until another button is pressed and a flag is set to false.  Currently I have to keep pressing the stop button since the program seems to lock.

System.Threading.Thread.Sleep(5000);

Thanks

Avatar of JimBrandley
JimBrandley
Flag of United States of America image

Sleep causes your thread to release the CPU for the requested time, so it would be dead for that period.

You can do what you want by using a System.Timer. Start it up, and it will fire an event each time the ttimer fires. You can do what you need in the event handler.

Jim
Avatar of jackjohnson44
jackjohnson44

ASKER

can you please privide some code, I don't really understand.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of JimBrandley
JimBrandley
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account