Einzelnen Beitrag anzeigen

TiGü

Registriert seit: 6. Apr 2011
Ort: Berlin
3.060 Beiträge
 
Delphi 10.4 Sydney
 
#11

AW: raise Exception.Create() feuert ApplicationEvents nicht aus TTask an

  Alt 11. Okt 2018, 16:19
Ich kann dir sowas noch anbieten:

Delphi-Quellcode:
unit Unit4;

interface

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

type
  TForm4 = class(TForm)
    Memo1: TMemo;
    procedure Memo1Click(Sender: TObject);
  private
    { Private declarations }
  public
    procedure DoIt;
    procedure LogText(const AText: string);
  end;

var
  Form4: TForm4;

implementation

{$R *.dfm}


procedure TForm4.DoIt;
var
  MyTask: ITask;
  MyProc, DoMyProc, HandleException: TProc;
begin
  MyProc := procedure
    var
      I: Integer;
    begin
      I := 0;

      while I < 1000 do
      begin
        if I = 900 then
        begin
          raise Exception.Create('Error Message: ' + TThread.Current.ThreadID.ToHexString);
        end;
        Inc(I);
      end;
    end;

  DoMyProc := procedure
    begin
      try
        MyProc;
      except
        on E: Exception do
        begin
          TThread.Synchronize(nil,
              procedure
            begin
              LogText(E.ClassName + ': ' + E.Message)
            end);
        end;
      end;
    end;

  MyTask := TTask.Run(DoMyProc);
  MyTask.Start;
end;

procedure TForm4.LogText(const AText: string);
begin
  Memo1.Lines.Add(AText);
end;

procedure TForm4.Memo1Click(Sender: TObject);
begin
  DoIt;
end;

end.
  Mit Zitat antworten Zitat