Solved

How to make a loop with FOR simulating a sleep ?

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

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 36

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now