mr-highball / ezthreads Goto Github PK
View Code? Open in Web Editor NEWsimple to use threading library
simple to use threading library
this used to work, however I during some refactors this must've been neglected. the main issue is that thread killing logic is inside the max runtime logic (which means if no max, then no force terminates will work). in addition to that, we need to verify this code actually works as it looks a little off to me now.
this was found out in #6
Hello!
I have main thread with form and progress bar.
To download some file I call function that create new thread.
Is it possible to call at any time some function with synchronisation with main thread?
procedure Download(Url: string);
var
DownloadThread: IEZThread;
begin
DownloadThread := NewEZThread();
DownloadThread.AddArg('downloadUrl', Url);
DownloadThread.Setup(BackgroundDownload);
DownloadThread.Settings.UpdateSynchronizeStopEvents(True);
DownloadThread.Events.UpdateOnStopCallback(BackgroundDownloadSetupComplete);
DownloadThread.Start();
end;
procedure BackgroundDownload(const AThread: IEZThread);
begin
// Here i have download object with progress callback that on each step has some chunk of data.
// So i have all data to report progress.
// But how from this thread (while it is running) i can report download progress to main thread?
// I'm looking for something like AThread.Synchronize( {some function to execute from main thread} );
end;
procedure BackgroundDownloadSetupComplete(const AThread: IEZThread);
begin
// Here we are in main thread
// We can get download results from AThread
end;
I saw solution with thread pool, but i'm curious is it possible with this approach.
Testing ezthreads_tester_ui
with lazarus 2.0.9 and fpc 3.2.
After pressing Do it
button main thread freezes after 10 sec.
Is there a way to set thread name?
Hello!
What is the best way to implement background worker, that will execute some method every 30 seconds?
And how to terminate worker by force when app closing?
Hey @mr-highball,
First I have to admit that I haven't dived into your framework's code to see if there is an example. I'm sorry if I'm asking for something already obvious on the code.
My ask is quite simple: On README.md
, add a simple example with main thread synchronization denoted by the use of the TThread.Synchronise()
procedure. That's it!!
Many thanks in advance!
Cheers,
Gus
Hi, thank you for your great units. The code is easy to use.
I'm running Ubuntu with FreePascal 3.2.0 and Lazarus 2.0.12 and I use the matter it to check if the KillThread(ThreadID)
behavior of the cthreads
has common issues.
I have the issue that the KillThread(ThreadID)
won't kill anything despite of giving a positive call result of 0, as written in the handbook. The thread continues the Execute
part (..see attached script TestAsync.zip).
I have used your test program ezthreads_tester.lpr
, extract the relevant part and added some timestamps (...see TestEzAsync.zip), to get a "second opinion" and to see if the KillThread
issue is a "feature" under Linux. It seems to me, that there are the same issues in both programs (TestAsync and TestEzAsync). As you see I set the sleep time of the thread to 10 s, the program should be killed after 50 ms but the the program is running through the complete 10 s defined by the sleep time (..see CONFIG TestEzAsync and OUTPUT TestEzAsync).
I got similar results with my clunky command loop test when i call kill
the execution process remains and jumps back to the next input prompt (OUTPUT TestAsync).
** Question 1** Is this behavior for cthread
Linux specific and KillThread(...)
malfunctioned?
** Question 2** Is there a possibility, to solve that issue BEYOND the permanent commented common solution, to flag the Execution procedure to stop at a certain point (in a loop for example)? I'd like to use the timeout-kill feature in a socket connection context (SQL request for example).
CONFIG TestEzAsync
LThread:=TEZThreadImpl.Create;
LThread
.AddArg('sleep',10000) // < -------------- HERE
.AddArg('start',Now)
.Settings
.UpdateMaxRuntime(50) // <------------ HERE
.UpdateForceTerminate(True) //non-graceful termination, but guarantees no dangling threads after timeout
.Thread
.Events
.UpdateOnStopNestedCallback(@CheckElapsed)
.Thread
.Setup(@Setup)
.Start;
OUTPUT TestEzAsync
TestForceKill: force kill start 30-12-99 13:27:07
TestForceKill::CheckElapsed::elapsed time - 51
TestForceKill::force kill finished 30-12-99 13:27:17 <------- 10 s left
OUTPUT TestAsync
------- START --------
PROMPT> kill
KILL.REQUEST: 30-12-99 13:46:18
KILL.RESULT: 0 0 - means OK 30-12-99 13:46:18 <------- should stop here
PROMPT> exit
----- READY ----------
```
in #14 an example was provided that called Await() on an ezthread during the destructor of an object that was triggered by a form.close(). the solution was to pump via Application.ProcessMessages() in a loop checking on the thread state = esStarted
Ac's
Notes
if Assigned(Application)
Does ezthreads support thread pool? If yes, is there any sample code for me to get started?
Hello mr-highball,
I tried to test your good lib and found the memory-leak.
I tested the library on Lazarus 2.0.4 / FPC 3.2.0
Thank you for such nice Lib.
Best Regards,
Richie.
A request was made in #14 to simplify synchronizing events to the UI.
AC's
Notes
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.