Solved

Cerating 2 thread classes with inherit betwen this 2 clases

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now