c# better sleep

Posted on 2007-10-01
Last Modified: 2009-10-03
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.



Question by:jackjohnson44
    LVL 22

    Expert Comment

    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.


    Author Comment

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

    LVL 22

    Accepted Solution

    1. Add this member to your form class:
    static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();

    2. In your form class, add this:
    // This is the method to run when the timer is raised.
    private static void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
       // Perform the action you want to take each time the timer fires.

    2. In the code where you perfom class-levei initialization, add this:
    // Add the event and the event handler for the method that will process the timer firing.
    myTimer.Tick += new EventHandler(TimerEventProcessor);

    3. Whenever you want to start the timer running, execute this:
    // Sets the timer interval to 10 seconds. (Interval is in ms)
    myTimer.Interval = 10000;

    4. When you want the timer to stop, execute this:

    You can get more information here:



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now