Solved

Sleep (Delay) in threads

Posted on 1999-01-24
21
411 Views
Last Modified: 2010-04-06
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
Comment
Question by:mrbird
  • 9
  • 5
  • 2
  • +4
21 Comments
 

Author Comment

by:mrbird
ID: 1363176
Edited text of question
0
 
LVL 17

Expert Comment

by:inthe
ID: 1363177
perhaps use a timer instead as a sleep affects all you app
Regards Barry
0
 

Author Comment

by:mrbird
ID: 1363178
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
 

Author Comment

by:mrbird
ID: 1363179
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
 
LVL 17

Expert Comment

by:inthe
ID: 1363180
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
 

Author Comment

by:mrbird
ID: 1363181
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1363182
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
 

Author Comment

by:mrbird
ID: 1363183
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
 
LVL 10

Expert Comment

by:Lischke
ID: 1363184
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
 
LVL 20

Expert Comment

by:Madshi
ID: 1363185
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 1363186
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
 

Author Comment

by:mrbird
ID: 1363187
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
 
LVL 20

Expert Comment

by:Madshi
ID: 1363188
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
 
LVL 3

Expert Comment

by:KE
ID: 1363189
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
 
LVL 20

Expert Comment

by:Madshi
ID: 1363190
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
 

Expert Comment

by:danGynn
ID: 1363191
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
 

Author Comment

by:mrbird
ID: 1363192
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
 
LVL 20

Expert Comment

by:Madshi
ID: 1363193
Yes, you're completely right...   :-)
May I answer the question?

Regards, Madshi.
0
 

Author Comment

by:mrbird
ID: 1363194
Yes, please answer the question.
0
 

Author Comment

by:mrbird
ID: 1363195
Yes, please answer the question. ;-)
0
 
LVL 20

Accepted Solution

by:
Madshi earned 100 total points
ID: 1363196
Thank you  :-)))
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

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

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