unit threadtest;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
LMessages, LCLIntf;
const
UM_RESULT = WM_USER + 1;
type
TWorker = class(TThread)
private
textbuffer: String;
protected
procedure Execute; override;
end;
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
worker: TWorker;
protected
procedure UMResult(var Message: TLMessage); message UM_RESULT;
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
worker := TWorker.Create(False);
end;
procedure TForm1.UMResult(var Message: TLMessage);
begin
Memo1.Lines.Add('Message: ' + String(Message.WParam));
end;
procedure TWorker.Execute;
begin
// Imitate to do something
Sleep(1000);
// Send message to UI thread
textbuffer := DateTimeToStr(Now);
LCLIntf.PostMessage(Form1.Handle, UM_RESULT, PtrInt(PString(textbuffer)), 0);
end;
end.
I just can't believe that this is so hard and there is no simple pattern. Communicating with a thread should be a basic feature in GUI programming, what Lazarus is for.It is a basic feature. Please test attached project. Count is just a dummy method, you should replace it with something useful. Feel free to ask more questions if you like.
Synchronize only works with a main thread, does it not?Why? synchronize is between threads, not only the main thread. Or it should be...
I never could get it to work on Android
Synchronize only works with a main thread, does it not?Yes.
Why? synchronize is between threads, not only the main thread.No. Is between a worker and the main thread. The AMethod specified in the parameter is called in the context of the main thread. Synchronize is blocking, the thread is waiting until the code is executed in the main thread. For asynchronous processing you can use Queue.
@ThaddyNot it is NOT. Basics. It is about the initiating thread - that happens to be often the main thread...... This is silly..If you missed that? back to school <grumpyish :D>QuoteWhy? synchronize is between threads, not only the main thread.No. Is between a worker and the main thread. The AMethod specified in the parameter is called in the context of the main thread. Synchronize is blocking, the thread is waiting until the code is executed in the main thread. For asynchronous processing you can use Queue.
PS: Synchronize /Queue is only needed when you wish to update visual controls.
Not it is NOT. Basics. It is about the initiating thread - that happens to be often the main thread...... This is silly..If you missed that? back to school <grumpyish :D>Too much talk is a waste of time, at least this is how I see it. Luckily everyone who is interested in the subject can do a quick google search.
I have trees of threads running...
In my solution, an additional thread is used to emulate the message queue.
You can see here: https://github.com/wadman/wthread
Unfortunately, some of the comments are in russian.
1) your WThread uses WMessage (not adviced on other platforms but windows)But it's magically working on other operating systems. :)
How about the examples here: https://www.freepascal.org/docs-html/rtl/classes/tthread.executeinthread.htmlExecuteInThread fires a single method in a created for this purpose thread and then stops that thread. That is far different from having a thread running a loop awaiting multiple orderS from main or other threads
WHAT is different? You can implement such a loop inside the procedure...How about the examples here: https://www.freepascal.org/docs-html/rtl/classes/tthread.executeinthread.htmlExecuteInThread fires a single method in a created for this purpose thread and then stops that thread. That is far different from having a thread running a loop awaiting multiple orderS from main or other threads
Stop barking at the moonlight, grab a book and learn instead of telling others to do so :P
WHAT...
Assignable typed constants are re-entrant..... No problems there...cool for you in your tiny amateur world :P can you please explain how you run a 'tree of threads' with yer poor knowledge Ima curious bird :D :D :D
bbbHAAHHAHAAHAHAHAH threadpool or deadpool that is the kuestion. A tree or a scrubland? hahahahahahaha
Not it is NOT. Basics. It is about the initiating thread - that happens to be often the main thread...... This is silly..If you missed that? back to school <grumpyish :D>
I have trees of threads running...
I also am ThaddyYou're definitely not Thaddy, though both of you are childish.
queen Mary, Bill Cody, king Richard, Vlad Tepes, and... your mom!Lol just like sam707.