Solved

How to make a loop with FOR simulating a sleep ?

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

806 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