Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#2

AW: BackgroundWorker [ab XE2]

  Alt 1. Jun 2015, 15:38
http://www.delphipraxis.net/185328-t...ktivieren.html mit dem BackgroundWorker

(Source und EXE im Anhang)

Delphi-Quellcode:
unit Form.Main;

interface

uses
  BackgroundWorker,

  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
  FMX.Controls.Presentation, FMX.Layouts;

type
  TService = class( TComponent )
  private
    FIsRunning: Boolean;
  public
    procedure Activate;
    procedure Deactivate;
    property IsRunning: Boolean read FIsRunning;
  end;

  TForm1 = class( TForm )
    Layout1: TLayout;
    Switch1: TSwitch;
    Label1: TLabel;
    AniIndicator1: TAniIndicator;
    procedure Switch1Switch( Sender: TObject );
    procedure FormShow( Sender: TObject );
  private
    FService: TService;
    FServiceWorker: TBackgroundWorker;
    procedure ServiceWorkerDoWork( Sender: TObject; e: TDoWorkEventArgs );
    procedure ServiceWorkerRunCompleted( Sender: TObject; e: TRunWorkerCompletedEventArgs );
  public
    procedure AfterConstruction; override;

  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}
{ TForm1 }

procedure TForm1.AfterConstruction;
begin
  inherited;
  // Service-Instanz
  FService := TService.Create( Self );

  // Der Service-EinAusSchalter
  FServiceWorker := TBackgroundWorker.Create( Self );
  FServiceWorker.OnDoWork := ServiceWorkerDoWork;
  FServiceWorker.OnRunWorkerCompleted := ServiceWorkerRunCompleted;

end;

procedure TForm1.FormShow( Sender: TObject );
begin
  Switch1.IsChecked := FService.IsRunning;
end;

procedure TForm1.ServiceWorkerDoWork( Sender: TObject; e: TDoWorkEventArgs );
begin
  if e.Argument.AsBoolean then
    FService.Activate
  else
    FService.Deactivate;
end;

procedure TForm1.ServiceWorkerRunCompleted( Sender: TObject; e: TRunWorkerCompletedEventArgs );
begin
  AniIndicator1.Visible := False;
  Switch1.Visible := True;

  Switch1.IsChecked := FService.IsRunning;
end;

procedure TForm1.Switch1Switch( Sender: TObject );
begin
  Switch1.Visible := False;
  AniIndicator1.Visible := True;
  FServiceWorker.RunWorkerAsync( Switch1.IsChecked );
end;

{ TService }

procedure TService.Activate;
begin
  if not FIsRunning then
  begin
    Sleep( 1000 );
    case Random( 2 ) of
      1:
        raise Exception.Create( 'Fehlermeldung' );
    end;
    FIsRunning := True;
  end;
end;

procedure TService.Deactivate;
begin
  if FIsRunning then
  begin
    Sleep( 500 );
    FIsRunning := False;
  end;
end;

end.
Angehängte Dateien
Dateityp: zip dp_185328.zip (2,05 MB, 59x aufgerufen)
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)

Geändert von Sir Rufo ( 1. Jun 2015 um 15:55 Uhr)
  Mit Zitat antworten Zitat