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.