Hi
Use a record with all the parameters you need and feed that to your thread / calcule-procedure...
If you use 'PostMessage()' for your comms, you shouldn't need synchronization, but YMMV.
Yes, with a pointer you can pass everything (as placed in the code even if in the case of the test it is not needed). You just have to add more code for each use, but you can't do it otherwise, putting the calculations in the thread.
...And don't forget -- Gui-code in threads (apart from mainthread) -- is
an ABSOLUTE NO-GO!!!
The LCL is NOT threadsafe!
Of course, this is just some preliminary code for testing.
I usually use a trick to execute a thread which I need to wait it to finish. I use a "Please wait" form that is called with ShowModal. This form's onShow event I create and run the thread.
When the thread finished, this form will handle by thread's onTerminate for example. Then, the form close itself and return ModalResult (eg. mrOK or mrCancel) based on thread result.
This way, the app won't get Not Responding flag from OS while the UI is blocked. It can be extended to allow aborting the thread execution.
Thanks @Xenno. The goal here is to automatically display a waiting form, but only when the calculation time exceeds n seconds. Otherwise, I find it annoying to have it disappear instantly or have to slow it down. In any case, the most annoying thing is seeing the dialog when it's not needed. This compensates, for example, when a user uses the program on a faster or slower computer.
A modal form is blocking for the main thread....
If the modal form is IN the thread, cdbc' s warnings apply.
TThread has class methods that can report back status, if required.
This is not difficult at all.
See the examples in the manual:
https://www.freepascal.org/docs-html/rtl/classes/tthread.executeinthread.html
Yes, the examples are clear. Thanks @Thaddy.
A modal form does not block the main or caller thread itself—it only blocks the UI of the main or caller form. This is why the operating system does not mark the application as Not Responding and protecting something for example unexpected button click in main form while the thread is still running.
The typical flow looks like this:
FormMain -> FormProgress -> WorkerThread -> FormProgress -> FormMain
FormProgress can display status or progress updates using a synchronized method from the running worker thread, while also providing a chance for user to cancel or terminate the worker thread.
I’ve been using this technique, for example, when loading large files, and it works well to keep the UI responsive while still allowing cancellable background processing.
Anyway "progress dialog" fits perfectly for modal form. But, yes, this is only a suggestion.
With the current implementation, I should also keep user actions "blocked." Given the use of "Application.ProcessMessages," a user tired of waiting might click around, causing problems

.