Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Cerating 2 thread classes with inherit betwen this 2 clases

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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

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…
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

618 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