Solved

How to make a loop with FOR simulating a sleep ?

Posted on 2009-05-14
4
178 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 37

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 500 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 37

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
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.

690 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