-5

In my program I have 2 threads, the first is running constantly while (isRunning) {

and for the second thread method, I need it to sleep for a minute while its true. The problem I am facing that with my current code is that I am getting a Not Responding for my program in the Task Manager.

How can I fix that, note that I am using Framework 2.0.

private void initialize() {
        MyWeb.connectToServer();
        commandThread = new Thread(checkForCommandThread); // The thread that I need not to freeze the program
        commandThread.Start();
    }

private void checkForCommandThread() {
        while (isRunning) {
            Thread.Sleep(10000);
            // while this thread is sleeping, the program should not be frozen
            // do the work here, after the sleep
        }
    }
Sartheris Stormhammer
  • 2,534
  • 8
  • 37
  • 81
  • May be you are looking for something like: http://stackoverflow.com/questions/15341962/how-to-put-a-task-to-sleep-or-delay-in-c-sharp-4-0 – Habib Mar 24 '15 at 18:24
  • 5
    You need to *not* block the UI thread. – Servy Mar 24 '15 at 18:25
  • This is for higher Frameworks, but discusses your problem in depth. http://stackoverflow.com/questions/16819447/alternative-to-thread-sleep-in-windows-form-apart-from-timer – Bob G Mar 24 '15 at 18:27
  • I see 3 downvotes, and none of you providing me with working answer for Framework 2.0 – Sartheris Stormhammer Mar 24 '15 at 19:30
  • Apparently your "second thread" is either the UI thread, or the UI thread itself is waiting on the second thread for some reason. In general, you should not be calling `Thread.Sleep()` _at all_. Even in .NET 2.0, there are asynchronous mechanisms that allow you to delay execution of code without tying up a whole thread. If you want a good answer, please provide [a good, _minimal_, _complete_ code example](http://stackoverflow.com/help/mcve) that clearly illustrates the context of your question. See http://stackoverflow.com/help/how-to-ask for advice on presenting a clear, answerable question. – Peter Duniho Mar 24 '15 at 19:54
  • there, I added more code, now please look it – Sartheris Stormhammer Apr 01 '15 at 09:22

2 Answers2

0

It seems I was getting this Not Responding on my program, because the other thread I had, that was running constantly, didn't had any Sleep in it, and was running as fast as possible, thus making the program Not Responding and using up to 13% of the CPU...

A simple Thread.Sleep(10); fixed everything

Sartheris Stormhammer
  • 2,534
  • 8
  • 37
  • 81
-1

Ok, assuming you have code like this

    public static bool _isRunning = false;

    private void button1_Click(object sender, EventArgs e)
    {
        _isRunning = true;
        var thread = new Thread(DoWork) {IsBackground = true};
        thread.Start();

        while (_isRunning)
        {
            //HERE GOES THE MAGIC
            Application.DoEvents();
        }
    }

    private static void DoWork()
    {
        System.Threading.Thread.Sleep(20000);
        _isRunning = false;
    }
cdmnk
  • 314
  • 1
  • 11
  • Here's big explanation about how and when you might use this 'DoEvents' thing: [link](http://stackoverflow.com/questions/5181777/use-of-application-doevents). But only fools can consider you always can 'not block ui thread'. – cdmnk Mar 24 '15 at 20:55