?
Solved

How to make a loop with FOR simulating a sleep ?

Posted on 2009-05-14
4
Medium Priority
?
180 Views
Last Modified: 2012-05-07
Hello guys,

I have problens with the sleep function because it makes my program stop.
I would like to make a look with for, but I don't know how to simulate the time as I pass to sleep:

Something like this:

For I := 0 to 3000 do Begin
  application.processmessage
End

I wonder if 3000 in my FOR is like sleep(3000)

Any clue is welcome
0
Comment
Question by:hidrau
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 24385666
maybe using threads or a TEvent would be easier
does the rest of the program have to keep running while you are doing something ?
0
 
LVL 19

Accepted Solution

by:
MerijnB earned 2000 total points
ID: 24385680
Can't you use a timer?

If not, you can try something like this, be aware that you should be careful with using Application.ProcessMessages() though.

uses DateUtils;
 
// ...
 
procedure MyDelay(Delay: integer);
var DateTime: TDateTime;
begin
 DateTime := Now();
 
 while MilliSecondsBetween(Now(), DateTime) < Delay) do
 begin
  Application.ProcessMessages();
  Sleep(1);
 end;
end;

Open in new window

0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 24385774
how to make 1 thread wait for another one, with a TEvent:
(pseudo code)

  mainprogram variable:
  E: TEvent;
  E := TEvent.Create(nil, True, False, 'eventwait');

  thread1 :
  has to wait for job finished in thread 2

  Done: Boolean;
  Done := False;
  while not Done do
  begin
    case E.WaitFor(3000) of // wait for 3000 msecs
      wrSignaled: Done := True; // job is done
      wrTimeout : ; // job is not finished yet
      wrAbandoned: ; // job was cancelled
      wrError:   : ; // job had error
      wrIOCompletion: ; // who knows what this is ?
    end;
  end;

  thread2 :
  repeat
    if CountMessages > 0 then
      E.SetEvent; // signal event, a message was found
  until False;
0
 
LVL 1

Author Closing Comment

by:hidrau
ID: 31581489
thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month12 days, 5 hours left to enroll

752 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