delphithread
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| delphithread [2024/07/23 15:35] – jwan | delphithread [2025/10/10 10:15] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 114: | Line 114: | ||
| The main form creates the thread with Button1Click and stops it with Button2Click. | The main form creates the thread with Button1Click and stops it with Button2Click. | ||
| This approach ensures that the thread can exit gracefully when the Stop method is called. | This approach ensures that the thread can exit gracefully when the Stop method is called. | ||
| + | |||
| + | |||
| + | Another Solution using Timer Event to do some background task: | ||
| + | |||
| + | < | ||
| + | unit BackgroundThread; | ||
| + | interface | ||
| + | uses Classes, SysUtils; | ||
| + | type | ||
| + | TBackgroundProcessThread = class(TThread) | ||
| + | protected | ||
| + | procedure Execute; override; | ||
| + | end; | ||
| + | |||
| + | implementation | ||
| + | |||
| + | procedure TBackgroundProcessThread.Execute; | ||
| + | begin | ||
| + | while not Terminated do begin | ||
| + | // Place your CPU-intensive process here | ||
| + | Sleep(10000); | ||
| + | end; | ||
| + | end; | ||
| + | |||
| + | end. | ||
| + | </ | ||
| + | < | ||
| + | uses BackgroundThread; | ||
| + | |||
| + | procedure TForm1.Timer1Timer(Sender: | ||
| + | var | ||
| + | BackgroundThread: | ||
| + | begin | ||
| + | BackgroundThread := TBackgroundProcessThread.Create(True); | ||
| + | BackgroundThread.FreeOnTerminate := True; | ||
| + | BackgroundThread.Start; | ||
| + | end; | ||
| + | </ | ||
delphithread.1721748910.txt.gz · Last modified: (external edit)
