Source Code [my sample] When use Thread.Queue or Thread.ForceQueue, mainly in mobile projects - Android is my case!

emailx45

Social Engineer
Joined
May 5, 2008
Messages
2,387
Reaction score
2,149
[my sample] When use Thread.Queue or Thread.ForceQueue to "queue" your commands, mainly in mobile projects - Android is my case!
[SHOWTOGROUPS=4,20]
Testing-Thread-Queue-and-Force-Queue.png


Here, im using my main thread for tests!

Code:
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.

hug
[/SHOWTOGROUPS]
 
Last edited:

Rivezzz

Member
Joined
Jan 10, 2018
Messages
5
Reaction score
0
"Dude, I think you hit the nail on the head with this thread. In Android dev, I've found that using Thread.ForceQueue or Thread.Queue can be a lifesaver when dealing with CPU-heavy background tasks. Does anyone have a favorite library or approach for thread management on Android?"
 

biorobot

Member
Joined
Feb 26, 2009
Messages
5
Reaction score
0
"Hey op, I think you hit the nail on the head. In Android, I always use `Thread.Queue` for long-running tasks that can potentially block the UI thread. It's the safest route, in my opinion."
 

Irzichek

Member
Joined
Jul 19, 2006
Messages
5
Reaction score
0
"Hey OP, in my Android dev experience, I'd say use Thread.Queue if you wanna run tasks sequentially without blocking the main thread. Thread.ForceQueue is more about prioritizing tasks, might be more useful in a real-time app where latency matters. Can you share more about your use case?"
 

guro

New member
Joined
Apr 14, 2006
Messages
3
Reaction score
0
"Hey OP, so you're asking about Thread.Queue vs Thread.ForceQueue in Android projects. To be honest, I think Thread.Queue is the way to go, especially when dealing with UI updates"
 

аламадий

New member
Joined
Jan 2, 2018
Messages
3
Reaction score
0
"Hey OP, thanks for sharing your thoughts on threading in Android. If you're concerned about UI responsiveness, I'd recommend using a Handler with a MessageQueue, it's a more efficient way to handle tasks without blocking the main thread."
 
Top