Is there any methods to reduce processor time usage for a non-main thread in .NET environment. The thread is used for getting informaion from a web-server all the time and it consumes all one-core processor time. I know I can use Thread.Sleep() but it doesn’t seem a nice solution for this task.
Share
Messing with priorities, etc, will not work. If you have 1 thread in your system that’s saying “I’ve got work to do, let me work”, and no other competing threads, windows will keep scheduling it.
You indicate that this thread is getting information from a web server. Presumably, it’s at least blocking whilst waiting for a response from the server, so hopefully not using CPU at that time. Unfortunately, it does sound like you’re polling for information, so
Thread.Sleepmay be best here – unless there’s some other out of band signal that could tell the thread when it should next poll.If the web server “knows” when the thread should next poll, it could hopefully be modified to include such information in its responses. You could then extract that information from the response, and at least set your
Sleepperiods to an appropriate value.Alternatively, if another thread will obtain information from elsewhere that indicates that a new request should be issued, then rather than
Thread.Sleep, you should use an Event (e.g.AutoResetEvent), andWaitOneon that signal, again specifying a suitable sleep period. That way, something else can awaken this sleeping thread whenever they think polling should again occur.