Solved

Cerating 2 thread classes with inherit betwen this 2 clases

Posted on 2006-11-01
1
131 Views
Last Modified: 2010-04-05
Hi I want to create 2 thread classes. First threat to be master thread and to use variables and procedures of second thread, but and second thread can use variables from first class.  
Can anyone send me a definition of these classes.

type
  TDownProcesThread = class(TThread,TSecondThread )
   
    a:Integer
   private

  protected

    procedure Execute; override;

  public

end;

type
  TSecondThread = class(TThread,TFirstThread)
 b:Integer
  private

  protected

    procedure Execute; override;

  public

end;

somewhere is code
a:=b;

and

b:=a   (???)


or give me some new idea for this

Thank you
0
Comment
Question by:nikola_mk
1 Comment
 

Accepted Solution

by:
pvlogchem earned 50 total points
ID: 17864575
Hi, if I read this correctly, you could use an interface for yout problem, like this:

type
  ISharedInterface = interface
    function GetA: Integer;
    procedure SetA(const aValue: Integer);

    property a: Integer read GetA write SetA;
  end

  TDownProcesThread = class(TThread, ISharedInterface)
  protected
    a:Integer
    function GetA: Integer;
    procedure SetA(const aValue: Integer);
    procedure Execute; override;
  end;

  TSecondThread = class(TThread, ISharedInterface)
  protected
    b:Integer
    function GetA: Integer;
    procedure SetA(const aValue: Integer);
    procedure Execute; override;
  end;

In code, you could then access the inteface of both threads like this:

procedure Example(aDownProcesThread: TDownProcesThread; aSecondThread: TSecondThread);
var
  aInterface: ISharedInterface;
  LocalInt: Integer;
begin
  aInterface := aDownProcesThread as ISharedInterface;
  LocalInt := aInterface.A;

  aInterface := aSecondThread as ISharedInterface;
  aInterface.A := LocalInt;
end;

If you want to prevent implementing the interface twice (or as much as you apply the interface to a class type), you could implement the interface in separate TObject-based class, and use a delegate in your thread-classes, like this:

type
  TSharedInterfaceImpl = class(TObject, ISharedInterface)
  protected
    a:Integer
    function GetA: Integer;
    procedure SetA(const aValue: Integer);
  end;

  TDownProcesThread = class(TThread, ISharedInterface)
  protected
    FSharedInterfaceImpl: TSharedInterfaceImpl;
  public
    // The ISharedInterface interface is delegated to this object.
    property SharedInterfaceImpl: TSharedInterfaceImpl read FSharedInterfaceImplimplements ISharedInterface;
  end;


Yet another solution would be a very dirty hack, but it would work also:

When both classes have the same base class, you could create an 'overlay', so that any one instance of these type can be casted to this 'overlay' type, making the members visible. In your example this would go something like this:

  TThreadOverlay = class(TThread)
  public
    a:Integer
  end;

procedure Example(aDownProcesThread: TDownProcesThread; aSecondThread: TSecondThread);
var
  LocalInt: Integer;
begin
  LocalInt := TThreadOverlay(aDownProcesThread).A;
  TThreadOverlay(aSecondThread).A := LocalInt;
end;

I hope one of these solutions helps. Good luck!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 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