Einzelnen Beitrag anzeigen

Schokohase
(Gast)

n/a Beiträge
 
#5

AW: Ignore mouseclicks during a task is running

  Alt 14. Jul 2019, 13:29
So würde es mit einem Task funktionieren
Delphi-Quellcode:
unit IgnoreMouseClicks.Forms.MainForm;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils,
  System.Variants,
  System.Classes,
  System.Actions,
  System.Threading,
  Vcl.Graphics,
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Dialogs,
  Vcl.ExtCtrls,
  Vcl.StdCtrls,
  Vcl.ActnList,
  Vcl.StdActns;

type
  TForm1 = class(TForm)
    ActionList1: TActionList;
    FileExitAction: TFileExit;
    WorkAction: TAction;
    IgnoreAction: TAction;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Panel1: TPanel;
    procedure FileExitActionUpdate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure IgnoreActionExecute(Sender: TObject);
    procedure IgnoreActionUpdate(Sender: TObject);
    procedure WorkActionExecute(Sender: TObject);
    procedure WorkActionUpdate(Sender: TObject);
  private
    FIsWorking: Boolean;

  public
    property IsWorking: Boolean read FIsWorking;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FileExitActionUpdate(Sender: TObject);
begin
  TAction(Sender).Enabled := not IsWorking;
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := not IsWorking;
end;

procedure TForm1.IgnoreActionExecute(Sender: TObject);
begin
  ShowMessage('Ignore was pressed');
end;

procedure TForm1.IgnoreActionUpdate(Sender: TObject);
begin
  TAction(Sender).Enabled := not IsWorking;
end;

procedure TForm1.WorkActionExecute(Sender: TObject);
begin
  FIsWorking := True;
  TTask.Run(
    procedure
    begin
      try

        // Do some work
        TThread.Sleep(5000);

      finally
        // final handler synchronized
        TThread.Synchronize(nil,
          procedure
          begin
            FIsWorking := False;
          end);
      end;
    end);
end;

procedure TForm1.WorkActionUpdate(Sender: TObject);
begin
  TAction(Sender).Enabled := not IsWorking;
end;

end.
  Mit Zitat antworten Zitat