Hi All!
Boreas, you need create Threaded procedures...
Please think like as an example: a File Searcher.
When you press the
search button , the program searches for files/folders, and shows in what folder is looking for, and the results are being added to the list. But the main screen of the application will never hang up, it works smoothly and even one can cancel the search (already in progress) to start a new search.
For it, you need get the current state (in the main form or in a
main threaded procedure) of your
running Threaded Procedures to interact with the data... After it (eg. data received), you can send to another
threaded procedure the order to send the data to the printer/panel/monitor to show the results. Thus, the time it takes the data received or send, as well as to display, everything is detached from the main thread runs in separate threads... (and really fast, because that procedures/functions not need refresh the main interface, you refresh it when you need).
This achieves no delays, but beware .... When you work with time limits, may be found to the problem of obtaining data that take longer than you think at some point, so you should consider that if the data was not received and processed in the estimated time, you must report adequately at the printer/panel/monitor.
Please, download the attached demo and see the procedure
class procedure TThreadControl.Thread_DoMyWork; on line 214 of the file unitthreadcontrol.pas ...
You will see
while True do begin // while you don't call TThreadControl.Stop();
Sleep(250);
if bStopFlag then break;
end;
If you write that loop (with Sleep) on the main form (by pressing a button), you will see that the interface will hangs... With threads, that doesn't happen.
In the attached demo, you will see
- How to start the thread
- How to send data before starting the thread, to perform 3 different actions (in the same thread)
- How to stop the thread
- How to get data from the thread: If it is running or not, the time running and in what point (of the thread) it is currently.
I apologize in advance, because the code refers to Threads is a bit complex to understand, but this is what you need...
{Apologies if I translated something wrong ... I speak spanish}
// Updated 7z attachement