Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call to Sleep() function freezes GUI

Status
Not open for further replies.

erossmu

Programmer
Oct 2, 2009
2
US
Basically, I have a procedure that is essentially a for loop and it goes through a process in which it first calls a windows beep then I delay the for loop with the Sleep() function.

Well, I also want to create an "Abort" button that will kill the for loop for whatever reason I choose. When I start the procedure with the for loop, the Sleep() function freezes the GUI so I cannot click my designated "Abort" button.

The question I have is, what can I do to circumvent this GUI lock? Is there an alternative to Sleep() that I can use or do I have to somehow create a separate thread?

Thanks in advance...
 
The best way to do this depends on what else is happening in this for loop. I presume calling windows.beep isn't the only thing you're doing...

To do what you're wanting (the abort button), though, you need to code your for loop as a while loop and have a second condition in place (somehow, there are 2 or 3 ways to do this) which will end the loop when the abort button triggers it.

Measurement is not management.
 
don't exagerate the timout of sleep in GUI windows. Use Application.ProcessMessages inbetween to keep the GUI responsive.

something like:

Code:
procedure Sleep1Second;

var Count : Integer;

begin
 for Count := 1 to 100 do
  begin
   Sleep(10);
   Application.ProcessMessages;
  end;
end;

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Yes, there are many more ways to obtain a delay, rather than using sleep(x).
e.g counting 'ticks' (system timer increments), using timer components, nesting loops, etc
sleep does just that, puts the cpu to sleep as far as your application (thread I suppose) is concerned.


Steve: N.M.N.F.
If something is popular, it must be wrong: Mark Twain
 
I got it working thanks to everyones help! Thank you all!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top