Einzelnen Beitrag anzeigen

TiGü

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

AW: System.Messaging.TMessageManager Asynchron?

  Alt 4. Mai 2018, 11:28
Delphi-Quellcode:
unit System.Messaging.Extended;

interface

uses
  System.SysUtils,
  System.Classes,
  System.Messaging,
  System.Threading;

type
  TMessageManagerHelper = class helper for TMessageManager
    procedure SendMessageToQueue(const Sender: TObject; AMessage: TMessage; ADispose: Boolean = True);
    procedure SendMessageAsync(const Sender: TObject; AMessage: TMessage; ADelayMS: Cardinal = 0; ADispose: Boolean = True); overload;
    procedure SendMessageAsync(const Sender: TObject; AMessage: TMessage; ADispose: Boolean = True); overload;
  end;

implementation

{ TMessageManagerHelper }

procedure TMessageManagerHelper.SendMessageAsync(const Sender: TObject; AMessage: TMessage; ADelayMS: Cardinal; ADispose: Boolean);
begin
  TTask.Run(
    procedure
    begin
      if ADelayMS > 0 then
      begin
        Sleep(ADelayMS);
      end;
      TThread.Queue(nil,
        procedure
        begin
          Self.SendMessage(Sender, AMessage, ADispose)
        end)
    end);
end;

procedure TMessageManagerHelper.SendMessageAsync(const Sender: TObject; AMessage: TMessage; ADispose: Boolean);
begin
  SendMessageAsync(Sender, AMessage, 0, ADispose);
end;

procedure TMessageManagerHelper.SendMessageToQueue(const Sender: TObject; AMessage: TMessage; ADispose: Boolean = True);
begin
  TThread.Queue(nil,
    procedure
    begin
      Self.SendMessage(Sender, AMessage, ADispose)
    end);
end;


end.
  Mit Zitat antworten Zitat