Delphi开发安卓APP中使用Thread.Queue 或 Thread.ForceQueue 队列Queue命令

Queue队列在Delphi中还是经常要用到的,这是在主线程中的Android实例代码,颈椎枕欢迎加入Delphi开发局QQ群:32422310

unit uVCL_FormMain;

interface

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

type
  TfrmFormMain = class(TForm)
    btnThread_Queue: TButton;
    btnThread_ForceQueue: TButton;
    Memo1: TMemo;
    procedure btnThread_QueueClick(Sender: TObject);
    procedure btnThread_ForceQueueClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    procedure prcMyLog(lText: string = '');
  public
    { Public declarations }
  end;

var
  frmFormMain: TfrmFormMain;

implementation

{$R *.dfm}

procedure TfrmFormMain.prcMyLog(lText: string = '');
begin
  if not(lText.IsEmpty) then
    Memo1.Lines.Add(lText);
  //
  Memo1.Lines.Add(format('%s - running my Memo Log code  <-----', [TimeToStr(now)]));
end;

procedure TfrmFormMain.btnThread_QueueClick(Sender: TObject);
begin
  //
  Memo1.Clear;
  //
  // forcing a "Queue" be used
  //
  // for test it, mark 2 break-point in this code:
  //
  TThread.ForceQueue(nil,
    procedure
    begin
      // command 0
      btnThread_Queue.Caption := 'btnThread_Queue ' + TimeToStr(now); { break point - later here! }
      //
      prcMyLog(format('%s - Queue - command 1', [TimeToStr(now)]));
      //
      prcMyLog(format('%s - Queue - command 2', [TimeToStr(now)]));
      //
      // Button1.Repaint;
    end);
  //
  prcMyLog(format('%s - Queue - command 3', [TimeToStr(now)]));
  //
  prcMyLog(format('%s - Queue - command 4', [TimeToStr(now)]));
  //
  prcMyLog(format('%s - Queue - command 5 = %s', [TimeToStr(now), StringOfChar('-', 40)]));
end; { break point - first occurrs here }

procedure TfrmFormMain.btnThread_ForceQueueClick(Sender: TObject);
begin
  //
  Memo1.Clear;
  //
  // in a main-thread, the call occurs imediatelly! - non-synchronization!
  //
  // for test it, mark 2 break-point in this code:
  //
  TThread.Queue(nil,
    procedure
    begin
      // command 0
      btnThread_ForceQueue.Caption := 'btnThread_ForceQueue ' + TimeToStr(now); { break point - first occurrs here! }
      //
      prcMyLog(format('%s - ForceQueue - command 1', [TimeToStr(now)]));
      //
      prcMyLog(format('%s - ForceQueue - command 2', [TimeToStr(now)]));
      //
      // Button1.Repaint;
    end);
  //
  prcMyLog(format('%s - ForceQueue - command 3', [TimeToStr(now)]));
  //
  prcMyLog(format('%s - ForceQueue - command 4', [TimeToStr(now)]));

  prcMyLog(format('%s - ForceQueue - command 5 = %s', [TimeToStr(now), StringOfChar('-', 40)]));
end; { break point - later here! }

procedure TfrmFormMain.FormCreate(Sender: TObject);
begin
  self.Position := TPosition.poScreenCenter;
  Memo1.Clear;
end;

end.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值