Solved

How to make a loop with FOR simulating a sleep ?

Posted on 2009-05-14
4
173 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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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
Virtuailstring tree compare node issue 14 125
find a node in VST 2 73
Sending files from  idTcpServer Socket to idTcpClient 2 44
Firemonkey allowing RTL on android 6 47
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…
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 shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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