Thema: Delphi Thread und Components

Einzelnen Beitrag anzeigen

Benutzerbild von arbu man
arbu man

Registriert seit: 3. Nov 2004
Ort: Krefeld
1.108 Beiträge
 
Delphi 7 Professional
 
#3

Re: Thread und Components

  Alt 1. Aug 2006, 20:57
Jo:
Delphi-Quellcode:
unit GLSI_DataModule;

interface

uses
  SysUtils, Classes, JvComponent, JvInterpreter_all, JvInterpreter;

type
  TInterpreterhread = class(TThread)
  protected
    procedure Execute; override;
  private
    PSender: TObject;
    PIdentifier: String;
    PValue: Variant;
    PArgs: TJvInterpreterArgs;
    PDone: Boolean;
  public
    Interpreter: TJvInterpreterProgram;
    procedure InterpreterGetValue(Sender: TObject; Identifier: String;
      var Value: Variant; Args: TJvInterpreterArgs; var Done: Boolean);
    procedure SyncGetValue();
    destructor Destroy; override;
  end;

type
  TGLSInterpreterModule = class(TDataModule)
    Interpreter: TJvInterpreterProgram;
    procedure InterpreterGetUnitSource(UnitName: String;
      var Source: String; var Done: Boolean);
    procedure InterpreterGetValue(Sender: TObject; Identifier: String;
      var Value: Variant; Args: TJvInterpreterArgs; var Done: Boolean);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
    Thread: TInterpreterhread;
    Lines: TStringList;
    procedure CompileCode;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

var
  GLSInterpreterModule: TGLSInterpreterModule;

implementation

{$R *.dfm}

{ TInterpreterhread }

procedure TInterpreterhread.Execute;
begin
  Interpreter.Compile;
  Interpreter.CallFunction('main', nil, []);
end;

{ TGLSInterpreterModule }

procedure TGLSInterpreterModule.CompileCode;
begin
  Thread:= TInterpreterhread.Create(True);
  Thread.Interpreter:= TJvInterpreterProgram.Create(nil);
  Thread.Interpreter.Pas.Clear;
  Thread.Interpreter.Pas.AddStrings(Lines);
  Thread.Interpreter.OnGetValue := Thread.InterpreterGetValue;
  Thread.Execute;
end;

procedure TInterpreterhread.InterpreterGetValue(Sender: TObject;
  Identifier: String; var Value: Variant; Args: TJvInterpreterArgs;
  var Done: Boolean);
begin
  if (AnsiSameText(Identifier, 'delay'))or
     (AnsiSameText(Identifier, 'sleep')) then
  begin
    sleep(Args.Values[0]);
    Done := true;
    exit;
  end else begin
    PSender := Sender;
    PIdentifier := Identifier;
    PValue := Value;
    PArgs := Args;
    PDone := Done;
    Synchronize(SyncGetValue);
    Done := PDone;
    Value := PValue;
  end;
end;

procedure TInterpreterhread.SyncGetValue();
begin
  GLSInterpreterModule.InterpreterGetValue(
      PSender, PIdentifier, PValue, PArgs, PDone);
end;

procedure TGLSInterpreterModule.InterpreterGetValue(Sender: TObject;
  Identifier: String; var Value: Variant; Args: TJvInterpreterArgs;
  var Done: Boolean);
begin
  //
end;
Björn
>> http://bsnx.net <<
Virtual DP Stammtisch v1.0"iw" am 19.09.2007 - ich war dabei!
  Mit Zitat antworten Zitat