I.T. Discussion Community!
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

October Edition
Subscribe now! It's Free!
Enter your email:

   ► MB LobbyPascal and Delphi Coding BoardDelphi Object PASCAL Topic   Print This     

Threads + sleep method

Threads + sleep method in Delphi Object PASCAL topic (part of our Pascal and Delphi Coding group).

Quick Search: Threads   Threads sleep   Threads sleep method  
Zoilus
 (Inactive)
-- USA
Hi,

I have a thread that needs some components from the Form it was called by. I thought of creating a new method Create that would require in his parameters these components, and then set them so I can use them in my thread (I suspect there is a better way to do it). But, when I change the Create method, the Execute method is never called... any idea why?

Also, I did some testing, and when I start a thread with a call to the Sleep method, even my main program sleeps... any idea how to fix this?
 Posted 18 years ago (Thread Starter)
Comment Quote
About Zoilus -Collapse +Expand
Visit Profile
Inactive member.
Member does not subscribe to this thread.
Email Not Verified!
Once email is verified, we will review and approve the account.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #3033, 1 replies
Thread Started 7/19/2001 7:28:00 AM
View Counter=3059
Location=-- USA 
Joined=18 years ago   MB Posts=2  
Most Recent Post
Scott Wehrly
Prestwood IT
 (Inactive)
Las Vegas, NV USA
Not really the way to use Threads in Delphi, I'm afraid.




I'll try to keep this short and to the point...




Adding a Create method to your TThread class interface is acceptable, but this method does not run the Thread - it's the Resume and Suspend methods on the TThread class that start and pause the Thread's Execute method, and the Thread's Terminate method that halts all execution.




Creating component dependencies between a Form class and a Thread class is not the right way to do this. Threads must be treated as stand-alone executables - not like child forms!




I think that this might be what's wrong with the sleep call. You're not really executing the Thread from the form the right way. If you're not using Resume and Suspend to execute the Thread, then it's just another method on your form, and the Sleep is suspending the form!




The right way to access components in the parent form is through synchronized methods. Direct access to a form's components from a thread causes problems with the Windows event handler. Synchronize is there to prevent these problems.




You need to create a callback-type method (procedure - not function!) on the form that will be called from the Thread. Give the form's method the parameters you need to update or populate the components on the form.




Then you need to create a parameterless method in your Thread class that can take values from the state variables in your Thread and pass them to the form's method. This new method is your synchronize method. It must be parameterless, because this is the argument type to the Synchronize call.




You use this synchronize method in the execution loop of your thread. When it's called, the thread is suspended in the CPU until the Windows event handler has received and processed the message to the form. Then, thread execution resumes.




Here is a very simple example of how to pass a status message to a list box on the form:




On the form...


interface
  ...
StatusBox : TListBox;
  ...

procedure UpdateStatus (message : String);
  ...

implementation
procedure TMyForm.UpdateStatus (message : String);
begin
StatusBox.Items.Add(message);
end;


In the thread...


interface
  ...
CurrentStatus : String;
procedure SendMyStatus;
  ...

implementation
Uses
frmMyForm;

  ...
procedure TMyThread.SendMyStatus;
begin
MyForm.UpdateStatus(CurrentStatus);
end;

// In the Execute method...

CurrentStatus := 'Running just fine';
Synchronize(SendMyStatus);
Sleep(100);






This should give you enough to get you going in the right direction.
 Posted 18 years ago
Comment Quote
About Scott Wehrly -Collapse +Expand
Visit Profile
Inactive member.
Member does not subscribe to this thread.
Old Account!
If this is your account, sign in to activate web presence data (sign in quarterly to keep active). Alternatively, you can subscribe to our monthly eMag with a valid email address.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #3038 (Level 1.1)  Reply to 3033
Thread Started 7/19/2001 12:19:00 PM
View Counter=2
Location=Las Vegas, NV USA 
Joined=17 years ago   MB Posts=442   KB Posts=19  

Revive Thread!

Add a comment to revive this old thread and make this archived thread more useful.

Write a Comment...
Full Editor
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1212A1
Enter key:
Icon: A Post    Thread    Idea    Important!    Cool    Sad    No    Yes    Includes a Link...   
Thread #3033 Counter
3059
Since 4/2/2008

Follow PrestwoodBoards on: 


©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech