Solved

Cerating 2 thread classes with inherit betwen this 2 clases

Posted on 2006-11-01
1
130 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
code issue 8 131
Press three keys together and trigger a function 3 57
tidtcpserver connection lost handle 2 87
DBCtrlGrid, Delphi, Scroll 7 15
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

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