Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 424
  • Last Modified:

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.
0
mrbird
Asked:
mrbird
  • 9
  • 5
  • 2
  • +4
1 Solution
 
mrbirdAuthor Commented:
Edited text of question
0
 
intheCommented:
perhaps use a timer instead as a sleep affects all you app
Regards Barry
0
 
mrbirdAuthor Commented:
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mrbirdAuthor Commented:
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
0
 
intheCommented:
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.
0
 
mrbirdAuthor Commented:
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
0
 
kretzschmarCommented:
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
0
 
mrbirdAuthor Commented:
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
0
 
LischkeCommented:
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
0
 
MadshiCommented:
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.
0
 
kretzschmarCommented:
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
0
 
mrbirdAuthor Commented:
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
0
 
MadshiCommented:
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.
0
 
KECommented:
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

0
 
MadshiCommented:
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.
0
 
danGynnCommented:
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.
0
 
mrbirdAuthor Commented:
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
0
 
MadshiCommented:
Yes, you're completely right...   :-)
May I answer the question?

Regards, Madshi.
0
 
mrbirdAuthor Commented:
Yes, please answer the question.
0
 
mrbirdAuthor Commented:
Yes, please answer the question. ;-)
0
 
MadshiCommented:
Thank you  :-)))
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 9
  • 5
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now