How to keep thread from hogging all CPU time

Posted on 2004-03-24
Last Modified: 2010-04-05
I have a thread which does effect transitions between 2 images. It is for kiosks where they cycle through advertising "slides" when there is no user activity. It runs 2 separate monitors from the same program, each displaying their own set of slides. Now that I am adding video into the sequences, I notice some effects hog the cpu time and the video stalls briefly (although the sound is not interrupted) while the effect is running.

The core of the effect code is...
        do next step in effect
    until terminated or effect_is_finished

how can I get it to release cpu time and not use it all so the video or otherthings do not briefly stall?

Code was written by employee not with us, but we know how the uncommecnted code works. What we need is:

1. First step, due to not much time available for this item, is just to get it to not hog all cpu time during an effect. A simple "application.processmessages" doesn't seem to do it. I guess because the video running is not issuing any messages to be processed.

2. The second step will be to have this core of the effect do a step in the effect only once evert 30th to 50th of a second. After all, there is no need to update the image on the screen for each and every step of a slide in or other effect, which could be up to 800 steps (screen size 800x600). It would be better to update the progress of the effect in larger chunks at the same rate as NTSC video - 30 times a second (or 60 if you count interlacing) - which would leave plenty of time for video and other things to run. So how would we implement a timer in the above code to do this?

It should be easy for someone familiar with writing threads. This is the only thread code in the kiosk project and as I said before, the employee that wrote the thread isn't with us and the rest of us have never had to get into the writing of threads.
Question by:lajko
  • 3
  • 3
  • 2
  • +1
LVL 11

Expert Comment

ID: 10674512
did you try setting the thread priority level to something lower

LVL 11

Expert Comment

ID: 10674536

 MyThread.Priority := tpLower;


Expert Comment

ID: 10674807
Insert a sleep(1) in the loop.

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.


Accepted Solution

gmayo earned 75 total points
ID: 10675804
Further to winexec, Sleep is usually the best way to avoid hogging the processor, it certainly works for me. Setting it to a lower priority will only have effect if the processor is busy doing other tasks.

Sleep(0) will relinquish the rest of the thread's time slice to other threads, but will return if nothing else is waiting.
Sleep(time) will suspend this thread for at least the specified number of milliseconds. In theory calling Sleep(10) will cause 100 sleep events per second, but in practice this could be 95-100 depending on processor load IMX.
Sleep(INFINITE) appears to be useless when other functions effectively do the same thing.

Geoff M.

Author Comment

ID: 10717692
winecex's answe was a start. However Geiff's answer was the most complete and explained what "sleep" did and how to control it. Adjusting the sleep time will allow the needed control over the effect timing and insure the videos on the other monitor are not interrupted. Now I can adjust the code for the effects to complete an effect in the specified time. Even though the timing would be approximate, that is OK for on-the-fly transitions between advertising slides.

Setting thread priority didn't properly stop/interrupt the effects once they got control of the processor.

Expert Comment

ID: 10721124

First of all, try to write others' names correctly...
Second, it's simply outrageous. My answer was not a start, my answer was an EXACT solution for your problem. Or you might suggest that without the explanation of gmayo it wouldn't work? LMAO

Anyway, I'm not responding often here, I don't care much about points, but what you did was *LAME*...


Author Comment

ID: 10730097
Sometimes the keys on the keyboard move around. Sorry about the type-O's.

Your answer was a good answer. It would work. however, when weighed against other answers, it was not as complete. Geoff's answer explained the parameter and indicated the results of various values to the parameter. When looking at all the answers, you'll find that Geoff acknowledged your answer and expanded upon it. That is why I felt your answer was a good start but Geoff provided a more complete answer. I did not intend to demean you answer in any way at all.

Some of us prefer answers that include an explaination. Because of Geoff's explaination, I was able to tweek the transitions to give the optimal performance for both the transitions and the video running on the other screen. Without what he wrote, I would be reseaerching "sleep" and would not have been able to fix the problem in just about a half an hour.

Sorry you felt my response was "lame". It was not intended to be that way.

I like complete answers, even if they cover what I already know. Being a programmer since the early 70's, I know the problems of giving "blind" support. You never know the level of expertise of the person asking the question. Some people are quite offended when you include the "obvious" information, while others are offended if you don't include the most basic information for the question. What to do?  Sometimes you are never right.

I like as much information as possible, and just read through what I already know.

Expert Comment

ID: 10735369
The facts are obvious:  you asked for a solution to your problem, you didn't ask for a detailed explanation, and my response solved completely your problem. By pushing that stupid sleep(1) in your thread the CPU usage became OK... Isn't it?
It would have been right to accept my comment as answer, and gmayo's comment as assisted answer, gmayo only detailed a little. Why that? Well, just because I responded correctly first, so are the forum rules.

"Some of us prefer answers that include an explaination." Yes, and if you prefer that, then it was your fault because you didn't specify it initially.

As I said, I really don't care about points, it's only a matter of principles. From my point what you did was wrong and that's all. Period.

Anyways, no problem :)

Author Comment

ID: 10735702
I feel it best to no longer continue this argumentative line of discussion.
If the forum moderators can close and lock this topic, I ask that you do so.

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to manage invalidate between two tvirtualstringtree in same form? 1 131
Delphi XE10 Round Image 2 162
Base1 Encode/Decode 3 82
PHP preg_replace code convert to Delphi 14 69
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

821 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