Solved

Talking to a thread

Posted on 1998-03-02
6
166 Views
Last Modified: 2010-04-06
I've used threads in the past to do an intensive process, like a sort or a file search.  In each case, they've terminated with a result.  That's fine.
But how do I talk to a thread to change its state, or to change its execution path, or to trigger it to do things?
I am thinking of an object that would normally be descended from TObject, but is now descended from TThread.  Some form of state machine, but one that does some heavy but short term processing or network access when the state changes.
I also need it to have some form of event-based reporting to the main thread.  Is there any way to make this work without it having to wait for the main thread?  In either case, how do you do it?
0
Comment
Question by:bcrotaz
  • 4
  • 2
6 Comments
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1359971
Hi

I'm not sure if I fully understand what you need, but here goes.

To get a thread to change its actions, you must put checks in its execute method.
E.g.
procedure TMyThread.Execute;
begin
  while (not Terminated) do
    if (MustDoThis) then
      DoThis
    else    
      DoThat;
end;

Your main form can then control the value of MustDoThis as the thread is running.

You can get the thread to trigger an event in the main form by getting the thread to call a procedure in the main form.
E.g.
TMyThread = class(TThread)
  procedure Execute; override;
end;

TMyForm = class(TForm)
public
  MyThread: TMyThread;
  procedure MyThreadIsDone(Sender: TObject);
end;

The form would create the thread when needed:
procedure TMyForm.Button1Click(Sender: TObject);
begin
  MyThread := TMyThread.Create(False);
end;

The form would also implement "MyThreadIsDone":
procedure TMyForm.MyThreadIsDone(Sender: TObject);
begin
  MyThread := nil;
  ShowMessage('My Thread is finished.');
end;

The thread would call the form's procedures when needed:
procedure TMyThread.Execute;
begin
  while (not Terminated) do
  Etc.....
  MyForm.MyThreadIsDone(Self);
end;

Does this help, or am I barking up the wrong tree?
JB
0
 
LVL 1

Author Comment

by:bcrotaz
ID: 1359972
OK....
Can you use an event property in the thread?
Just to make the external procedure calling easier...
If so would you use:

private
  FMyEvent: TNotifyEvent;

...

procedure FireMyEvent;
begin
  if Assigned(FMyEvent) then
    FMyEvent(Self);
end;

...
...
Synchronize(FireMyEvent);

Would the above work?
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1359973
Your code will work fine.

FMyEvent is private, so cannot be accessed from outside.  You can declare the event as a property like a normal component:

type
  TMyThread = class(TThread)
  private
    FMyEvent: TNotifyEvent;
    procedure FireMyEvent;
  public
    property MyEvent: TNotifyEvent read FMyEvent write FMyEvent;
    procedure Execute; override;
  end;

Your calling form could then set the event as follows:
  with TMyThread.Create(True) do begin
    MyEvent := DoMyThreadDone;
    Resume;
  end;

The form would then implement DoMyThreadDone:
procedure TForm1.DoMyThreadDone(Sender: TObject);
begin
  ShowMessage('Thread Done');
  // Note: The thread still exists at this point!!
  // Thus you can access it via "TMyThread(Sender)"
end;


Your "FireMyEvent" would remain unchanged...

JB
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 5

Expert Comment

by:JimBob091197
ID: 1359974
Hi bcrotaz,

Did you come right with your thread events?

JB
0
 
LVL 1

Author Comment

by:bcrotaz
ID: 1359975
Yup.

That's great, thanks.
0
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 200 total points
ID: 1359976
K, then, I assume I can answer this question too...

Cheers,
JB
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

786 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