delphithread
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| delphithread [2024/07/20 17:34] – jwan | delphithread [2025/10/10 10:15] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | Note: | + | Note: need to read more about using TThread.Synchronize or TThread.Queue |
| - | | + | |
| | | ||
| < | < | ||
| Line 46: | Line 45: | ||
| begin | begin | ||
| TMyThread.Create(False); | TMyThread.Create(False); | ||
| - | |||
| end; | end; | ||
| Line 55: | Line 53: | ||
| end. | end. | ||
| - | |||
| </ | </ | ||
| Line 61: | Line 58: | ||
| Define a flag in the thread class: | Define a flag in the thread class: | ||
| - | type | + | < |
| - | TMyThread = class(TThread) | + | |
| - | private | + | |
| - | FStop: Boolean; | + | FStop: Boolean; |
| - | protected | + | |
| - | procedure Execute; override; | + | procedure Execute; override; |
| - | public | + | |
| - | procedure Stop; | + | procedure Stop; |
| end; | end; | ||
| + | </ | ||
| Implement the Execute method to check the flag: | Implement the Execute method to check the flag: | ||
| + | < | ||
| procedure TMyThread.Execute; | procedure TMyThread.Execute; | ||
| begin | begin | ||
| - | while not Terminated and not FStop do | + | while not Terminated and not FStop do begin |
| - | | + | // codes here // |
| - | // Your task code here | + | |
| - | Sleep(100); | + | |
| end; | end; | ||
| end; | end; | ||
| + | </ | ||
| Implement the Stop method to set the flag: | Implement the Stop method to set the flag: | ||
| + | < | ||
| procedure TMyThread.Stop; | procedure TMyThread.Stop; | ||
| begin | begin | ||
| FStop := True; | FStop := True; | ||
| end; | end; | ||
| + | </ | ||
| Create and stop the thread from the main form: | Create and stop the thread from the main form: | ||
| + | < | ||
| var | var | ||
| MyThread: TMyThread; | MyThread: TMyThread; | ||
| Line 105: | Line 106: | ||
| end; | end; | ||
| end; | end; | ||
| + | </ | ||
| In this example: | In this example: | ||
| Line 113: | 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.1721496887.txt.gz · Last modified: (external edit)
