Einzelnen Beitrag anzeigen

fajac

Registriert seit: 1. Jul 2009
60 Beiträge
 
#6

Re: Thread - Synchronize - 2 Forms - Verständnisproblem

  Alt 1. Jul 2009, 10:28
Wenn du die Anzeigen auf Form2 partout in einem eigenen Thread machen willst, gibt es einen kleinen Trick, der dir vielleicht hilft: Man kann ein zweites (VCL)Formular im Kontext eines Threads mit Hilfe eines zusätzlichen Application-Objekts erzeugen und im Thread selbst die Nachrichtenwarteschlange abarbeiten. Das Formular ist dann komplett losgelöst vom Hauptthread und kann dann auch im eigenen Thread seine Controls aktualisieren etc.
Du kannst dann allerdings nicht direkt vom Hauptformular aus die entsprechende Methode im zweiten Formular aufrufen. Eine passende Windows-Nachricht schafft aber Abhilfe.
Hier ein Beispiel dazu :
Hauptformular
Delphi-Quellcode:
interface

{...}

type
  TFormThread = class (TThread)
  private
    FApplication : TApplication;
  protected
    procedure Execute; override;
  public
    FForm : TForm2;
    procedure OnNotify (Sender : TObject);
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    FFormThread : TFormThread;
    FTerminateByClose : Boolean;
    procedure OnTerminate (Sender : TObject);
  end;

{...}

implementation

{...}

{TForm1}

procedure TForm1.FormCreate(Sender: TObject);
begin
  FTerminateByClose := False;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if not Assigned (FFormThread) then
  begin
    FFormThread := TFormThread.Create(True);
    FFormThread.FreeOnTerminate := True;
    FFormThread.OnTerminate := OnTerminate;
    FFormThread.Resume;
  end;
end;

procedure TForm1.OnTerminate(Sender: TObject);
begin
  if not FTerminateByClose then
    FFormThread := nil;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  If Assigned (FFormThread) then
  begin
    FTerminateByClose := True;
    SendMessage (FFormThread.FForm.Handle, WM_CLOSE, 0, 0);
    FFormThread.FreeInstance;
    FFormThread := nil;
  end;
  Action := caFree;
end;

{ TFormThread }

procedure TFormThread.Execute;
begin
  FApplication := TApplication.Create(nil);
  try
    FApplication.Initialize;
    FForm := TForm2.Create(FApplication);
    FForm.OnNotifyParent := OnNotify;
    FForm.Show;
    while (not Terminated) do
    begin
      Sleep (1);
      FApplication.ProcessMessages;
    end;
    FApplication.ShowHint := False;
    FApplication.Destroying;
    FApplication.DestroyComponents;
  finally
    FreeAndNil (FApplication);
  end;
end;

procedure TFormThread.OnNotify(Sender: TObject);
begin
  Terminate;
end;
Formular2 :

Delphi-Quellcode:
interface

{...}

type
  TForm2 = class(TForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    FOnNotify : TNotifyEvent;
  public
    property OnNotifyParent : TNotifyEvent read FOnNotify write FOnNotify;
  end;

{...}

implementation

{TForm2}

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caHide;
  if Assigned (FOnNotify) then
    FOnNotify(Self);
end;
Mit dem Button in Formular1 wird das zweite Formular erzeugt und angezeigt.
  Mit Zitat antworten Zitat