Link to home
Start Free TrialLog in
Avatar of mrbird
mrbird

asked on

Sleep (Delay) in threads

How can I do Sleep in the threads (about 3 threads), which the function sleep will not effect each other. And also don't lose control to the program interface control.
I had tried Sleep function but when one thread call Sleep, other threads stop and wait for it.
(I can not use Application.ProcessMessages because I call it from thread)
I would like a sleep function that stop only that thread and do not effect to other and also the main program. So user can control the program.
Avatar of mrbird
mrbird

ASKER

Edited text of question
perhaps use a timer instead as a sleep affects all you app
Regards Barry
Avatar of mrbird

ASKER

How to apply TTimer to my program, I never use timer this way..
My code look like this :-

for i:=1 to Duration do
begin
  Walk;  // call function Walk
  Sleep(speed);
end;

Thank you
mrbird
Avatar of mrbird

ASKER

How to apply TTimer to my program, I never use timer this way..
My code look like this :-

for i:=1 to Duration do
begin
  Walk;  // call function Walk
  Sleep(speed);
end;

Thank you
mrbird
sorry im an idiot i was thinking of something else :-)
although i think you possibly could but you'd have to use a while loop while the timers enabled and stop your walk function.
not sure how though.
Avatar of mrbird

ASKER

How to apply TTimer to my program, I never use timer this way..
My code look like this :-

for i:=1 to Duration do
begin
  Walk;  // call function Walk
  Sleep(speed);
end;

Thank you
mrbird
Avatar of kretzschmar
HI mrbird,

instead of sleep you can do this:

Procedure ThreadSleeping(Duration :TTime);
var D : TDateTime;
begin
  D := Now + Duration;
  repeat
     { You can do something here }
  until Now > D;
end;

.

for i:=1 to Duration do
begin
  Walk;  // call function Walk
  ThreadSleeping(StrToTime('00:00:10')); {Sleep for 10 Seconds}
end;

.

meikl
Avatar of mrbird

ASKER

Hi meikl,
I tried what you told me.. it freeze my application, the screen doesn't refresh and I can not click any button on it. The CPU utiliztion is 100%.
Any way to fix this..
thank you
mrbird
There msut be something wrong with your threads. The sleep function does only suspend the calling thread for the specified time. Does your thread create own windows? Do the threads depend on each other?

Ciao, Mike
Hi mrbird,

I really don't understand why Sleep should have an effect on another thread. It doesn't in my programs...   :-)
I think you should try to find out, why the other threads stop working when you Sleep one thread. It should not be this way!

Regards, Madshi.
Hi mrbird, hi others,

i've not tested it, but set the Thread.priority to tpIdle before go sleeping and after sleeping to the old priority.

By reading the others comments,  it seems the problem is on another position in  your code. Maybe it helps, if you post more code here.

meikl
Avatar of mrbird

ASKER

Hi all, this is my create thread part of program

for i:=1 to 6 do
begin
  if pathList[i] <> Nil then
  begin
    Inc(ThreadsRunning);  {Count the number of running threads}
    HThread[i] := WalkThread.Create(i, PathList[i]); {Initial with 1 suspend, send parameter "i" as identification of threads and "PathList[i]" as the path of Thread i }
    HThread[i].OnTerminate := ThreadDone;
    HThread[i].Resume;    
  end;
end;

and I create the thread class

unit SimThread;
 :
 :
type
  WalkThread = class(TThread)
  private
    :
    :
  end;

implementation

constructor WalkThread.Create(i: Integer; var path: PathListPtr);
begin
  Index := i;
  PathList := Path;
  inherited Create(True);
end;

procedure WalkThread.Execute;
begin
  repeat
    Synchronize(UpdateForm);
  until PathList = Nil;
end;

procedure WalkThread.UpdateForm;
  :
begin
    :
  for i:=1 to duration do
  begin
    Walk(MainForm.People[i]);
    Sleep(Speed);
  end;
    :
end;

Is it problem with Synchronize statement??

Thank you
mrbird
Hi mrbird,

the problem is the Synchronize statement!!!
Your Synchronize(UpdateForm) call sends a message to the main thread. The main thread receives this message, then calls UpdateForm. That means the code in UpdateForm is ALWAYS called in the context of the main thread. If you call Sleep, it Sleeps the main thread.
It seems to me that none of your threads does anything senseful inside the thread. All what your threads do is send messages to the main thread. And the main thread does all the work.
I really hate it, that the VCL is not reentrant (thread-safe). That's where all the **** comes from...   :-(

Regards, Madshi.
I aggree with you madshi on the thread-safe part of Delphi - I hate it too.

Mrbird - try to look at the synchronization objects of the Win API. So instead of sleeping your threads, let them wait for an event.

Here's the functions:
CreateEvent
ResetEvent
SetEvent
CloseEvent
WaitForSingleObject

I think, mrbird's threads don't make sense at all in the moment, because they do nothing but tell the main thread to do the work.

So IMHO he should either find a way of doing what he needs to do without calling the Synchronize method (then he can use Sleep) or he should drop the threads and do all the work in the main thread directly (then he needs no Sleep)...   :-(((

Regards, Madshi.
If you only want to call sleep so that the application updates the form and allows user input, call Application.ProcessMessages from within the Synchronized procedure instead of the sleep.  Then if there are any user commands, they will get called.
Avatar of mrbird

ASKER

Thank you to all comments, now I know what's wrong with it and how to fix it. If I sleep outside "Synchronize" function and when it really need to update I just call "Sync."  for a short function. I have no compiler with me now but I think it should work. What do you think??
Regards,
mrbird
Yes, you're completely right...   :-)
May I answer the question?

Regards, Madshi.
Avatar of mrbird

ASKER

Yes, please answer the question.
Avatar of mrbird

ASKER

Yes, please answer the question. ;-)
ASKER CERTIFIED SOLUTION
Avatar of Madshi
Madshi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial