[.net] Looping uses 100% CPU???

Started by
15 comments, last by GameDev.net 18 years, 8 months ago
I'm using C# 2005 and I've just made a simple while loop with an Application.DoEvents() but I went to task manager and the CPU is at 100%!! Why is this?! What can I do to minimise this usage?
Advertisement
check out the Thread.Sleep method here.

quote: "Specify zero (0) to indicate that this thread should be suspended to allow other waiting threads to execute"
You're using 100% CPU because your program never stops to give its CPU time back to the OS. There's nothing really wrong with this; in fact, most games will use up as much CPU as they can, since you usually want to let the game run as fast as possible. Just remember to call Thread.Sleep when your app is minimized or the user alt-tabs out of it. Using 100% CPU when the user is not playing your game is impolite.
I'm not making a game, and I should have stated that. It's just a standard application. The loop exists to process incoming TCP/IP data. The loop repeats and polls for incoming data. If the data exists then it will process the command and so on until the program is ended. Of course as the whole time this program is using 100% CPU. What would I do in a case like this? Run it as a background process? Or would sleeping the thread be enough?
Quote:Original post by Kryptus
I'm not making a game, and I should have stated that. It's just a standard application. The loop exists to process incoming TCP/IP data. The loop repeats and polls for incoming data. If the data exists then it will process the command and so on until the program is ended. Of course as the whole time this program is using 100% CPU. What would I do in a case like this? Run it as a background process? Or would sleeping the thread be enough?


You're polling the socket which is the worst possible thing you could do. Use select(), WSAAsyncSelect() or something like that.



Nevertheless, the idea is the same as a game. Your program is not event-based so you should expect to see near 100% CPU usage. If you started another application that used the CPU constantly, then you would notice windows would split up the CPU usage between each task.

If you still want to lower your CPU usage, perhaps you should tell windows not to update your program as often. Be sure before you do this though that your applicataion will still run correctly.

You can do this by creating a timer that gets executed a specific time intervals. A thread (which is essentially a timer) would also work. Or as rollo said, call Sleep when you determine that your program is taking up too much time.

However, by doing this you will notice that your System Idle process will get an increasing boost in CPU usage. When you see this, windows is telling you, "Hey I finished all my tasks early and have nothing to do." It's not a bad thing, but you should be aware of it.

One final thought that comes to mind is running your application as a service.
....[size="1"]Brent Gunning
What's wrong with polling the socket? I also just tried out the thread sleep command in my loop.

Start Loop
Process Windows messages
Sleep for 10 milliseconds

Process incoming network traffic
End Loop

I tried this out and I haven't had any problems. My program works find and I have between 0% and 2% CPU usage. Is there something I should be aware of or is it safe to do this?
Quote:Original post by Kryptus
What's wrong with polling the socket? I also just tried out the thread sleep command in my loop.

Start Loop
Process Windows messages
Sleep for 10 milliseconds

Process incoming network traffic
End Loop

I tried this out and I haven't had any problems. My program works find and I have between 0% and 2% CPU usage. Is there something I should be aware of or is it safe to do this?


That should be fine, I think. Since the code in the loop will only take a fraction of a millisecond, the sleep function (10 ms) leaves plenty of time for the CPU. If you use application.doevents or sleep(0), then as soon as the messages are processed, the loop continues.
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute
Quote:Original post by Kryptus
What's wrong with polling the socket? I also just tried out the thread sleep command in my loop.

Start Loop
Process Windows messages
Sleep for 10 milliseconds

Process incoming network traffic
End Loop

I tried this out and I haven't had any problems. My program works find and I have between 0% and 2% CPU usage. Is there something I should be aware of or is it safe to do this?


It really depends on what you're doing. If it's a game client, then it might be okay to poll the socket on every frame. If you're trying to write an MMO server, you probably will want more real-time response than what you can get with polling like that.

But, what might also happen is it will force the OS to keep those pages of your process from swapping out when nothing is going on. This might be a big deal if memory is getting low, because it can possibly result in more paging than you would like, which could slow you down a lot.


Why don't you just use asynchronous calls? Sounds to me as if it'd make what you are trying to do a lot easier. You could still have just one thread which actually handles the data, basically you could just put all incoming data into a queue and then signal to the handling thread when it has work to do (for example using a ManualResetEvent).

Sounds a lot cleaner to me, at least, as you avoid having to constantly check for input, and your loop will only do something when there is actually data that must be handled.

This topic is closed to new replies.

Advertisement