Recent

Author Topic: how to run code in background  (Read 1423 times)

rcmz

  • Full Member
  • ***
  • Posts: 114
how to run code in background
« on: January 03, 2024, 12:43:05 am »
hi,

How can I run code in background?

In Delphi I uses TBackgroundWorker

Any ideas?

TIA
rcmz

cdbc

  • Hero Member
  • *****
  • Posts: 1074
    • http://www.cdbc.dk
Re: how to run code in background
« Reply #1 on: January 03, 2024, 08:07:05 am »
Hi
I think, underneath "TBackgroundWorker" is a thread, sooo use a thread  8)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: how to run code in background
« Reply #2 on: January 03, 2024, 08:08:03 am »
TThread?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

AlanTheBeast

  • Sr. Member
  • ****
  • Posts: 348
  • My software never cras....
Re: how to run code in background
« Reply #3 on: January 03, 2024, 04:02:37 pm »
TThread?

Without objections, I would do:

Code: Pascal  [Select][+][-]
  1. Function Achunkofrealpascal (p:pointer):ptrint;
  2. VAR
  3.    z:somerecord;
  4. BEGIN
  5.    z := somerecord(p^);
  6.    While not z.Shutdown do
  7.       begin
  8.          stuff;
  9.          sleep(z.SleepPeriod);
  10.       end;
  11.    exit(0);
  12. END;
  13.  
  14. //somewhere in main:
  15.   With BlockOfData do
  16.     begin
  17.        Shutdown := false;
  18.        SleepPeriod := 100;  //ms
  19.        Otherstuff := ...
  20.    end;
  21.   TID := BeginThread(@Achunkofrealpascal, @Blockofdata);

Notes: use flags or data in the referenced record to send signals into/out of the background thread.

You can kill the thread using TID as a reference, or (in the case above) by setting Shutdown to true.
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

Jiyahana

  • New member
  • *
  • Posts: 9
Re: how to run code in background
« Reply #4 on: January 03, 2024, 04:16:39 pm »
You can use TThread specifically by creating a descendant of the TThread class. Implement the code that needs to run in the background within the execute method of the TThread descendant.

Example.

Quote
uses
  System.Classes;

type
  TMyBackgroundThread = class(TThread)
  protected
    procedure Execute; override;
  end;

procedure TMyBackgroundThread.Execute;
begin
  // Perform the background operations here
  // Ensure synchronization if accessing UI elements
end;

Instantiate and start the background thread where needed in your Delphi application.

Quote
var
  MyThread: TMyBackgroundThread;
begin
  MyThread := TMyBackgroundThread.Create(True); // Create suspended
  MyThread.FreeOnTerminate := True; // Auto-free thread when done
  MyThread.Start; // Start the background thread
end;


I hope example will help.

Thaddy

  • Hero Member
  • *****
  • Posts: 14359
  • Sensorship about opinions does not belong here.
Re: how to run code in background
« Reply #5 on: January 03, 2024, 04:56:05 pm »
Not really.
Much easier are the options possible for TThread,ExecuteInThread()
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

rcmz

  • Full Member
  • ***
  • Posts: 114
Re: how to run code in background
« Reply #6 on: January 03, 2024, 06:07:53 pm »
Thx,

I will try it.


KodeZwerg

  • Hero Member
  • *****
  • Posts: 2049
  • Fifty shades of code.
    • Delphi & FreePascal
Re: how to run code in background
« Reply #7 on: January 03, 2024, 11:42:32 pm »
If its something where you not need to take much care about ...
Code: Pascal  [Select][+][-]
  1. procedure ThreadProc;
  2. begin
  3.   // all code that needs to run threaded belong in here
  4.   Sleep(5000);
  5.   Form1.Button1.Enabled := True;
  6. end;
  7.  
  8. procedure TForm1.Button1Click(Sender: TObject);
  9. var
  10.   AProc: TProcedure;
  11. begin
  12.   Button1.Enabled := False;
  13.   AProc := @ThreadProc;
  14.   TThread.CreateAnonymousThread(AProc).Start;
  15. end;
Have in mind, code will run to its endphase even if you closed your process somehow.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Thaddy

  • Hero Member
  • *****
  • Posts: 14359
  • Sensorship about opinions does not belong here.
Re: how to run code in background
« Reply #8 on: January 04, 2024, 10:33:22 am »
I would still also save the result of the not so anonymous thread, since it returns a valid TThread that can be used later when needed. Comes to mind TThread.CheckTerminated.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: how to run code in background
« Reply #9 on: January 04, 2024, 10:57:51 am »
I would still also save the result of the not so anonymous thread, since it returns a valid TThread that can be used later when needed. Comes to mind TThread.CheckTerminated.
Nevermind WaitFor and OnTerminated
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018