DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: webb123
今日帖子: 3
在线用户: 2
导航: 论坛 -> DELPHI技术 斑竹:liumazi,sephil  
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2020/5/7 1:08:26
标题:
[my sample] When use Thread.Queue or Thread.ForceQueue to 浏览:939
加入我的收藏
楼主: [my sample] When use Thread.Queue or Thread.ForceQueue to "queue" your commands, mainly in mobile projects - Android is my case!

按此在新窗口浏览图片


Here, im using my main thread for tests!

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.
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行64.45313毫秒 RSS