Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Talking to a thread

Posted on 1998-03-02
6
Medium Priority
?
172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 800 total points
ID: 1359976
K, then, I assume I can answer this question too...

Cheers,
JB
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

722 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