?
Solved

Cerating 2 thread classes with inherit betwen this 2 clases

Posted on 2006-11-01
1
Medium Priority
?
134 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
[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
1 Comment
 

Accepted Solution

by:
pvlogchem earned 100 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

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…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month10 days, 19 hours left to enroll

770 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