TThreadPriority (Delphi)
Description
This example shows how to create a thread and start it running at a lower priority than the main execution thread. Set the thread's FreeOnTerminate property to True, so that there is no need to free the thread when it finishes. The Execute procedure must be overridden in the extension class of TThread or else an "Abstract Error" will result. To create the extension, choose the File > New > Other > Thread Object menu.
Code
procedure TForm1.Button1Click(Sender: TObject);
begin
if (mythreadRunning = False) then
begin
mythreadRunning := True;
MyProcess := TMyThread.Create(True); { Create suspended-second process does not run yet. }
MyProcess.FreeOnTerminate := True; { You do not need to clean up after termination. }
MyProcess.Priority := tpLower; { Set the priority to lower than normal. }
MyProcess.Resume; { now run the thread }
end
else
MessageDlg('This thread is still running. You are going to hurt yourself!', mtInformation, [mbOk], 0);
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
if mythreadRunning then MyProcess.Terminate();
end;
procedure TMyThread.Execute;
var
I: Integer;
begin
Form1.Memo1.Lines.Add('Process has been running for this many seconds:');
for I := 0 to 10 do
begin
if Terminated then break;
Form1.Memo1.Lines.Add('Low priority process ' + InttoStr(I));
Sleep(1000);
end;
mythreadRunning := False;
end;
procedure TYouThread.Execute;
var
I: Integer;
begin
Form1.Memo3.Lines.Add('Second low priority process has been running for this many seconds:');
for I := 0 to 10 do
begin
if Terminated then break;
Form1.Memo3.Lines.Add('Second low priority process ' + InttoStr(I));
Sleep(1000);
end;
youthreadRunning := False;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
I: Integer;
begin
Form1.Memo2.Lines.Add('Do some work in the main process for 10 seconds:');
for I := 0 to 10 do
begin
Form1.Memo2.Lines.Add('Main process ' + InttoStr(I));
Sleep(1000);
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
if (youthreadRunning = False) then
begin
youthreadRunning := True;
YouProcess := TYouThread.Create(True); { Create suspended-second process does not run yet. }
YouProcess.FreeOnTerminate := True; { You do not need to clean up after termination. }
YouProcess.Priority := tpLower; { Set the priority to lower than normal. }
YouProcess.Resume; { Now run the thread. }
end
else
MessageDlg('This thread is still running. You are going to hurt yourself!', mtInformation, [mbOk], 0);
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
if youthreadRunning then YouProcess.Terminate();
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
mythreadRunning := False;
youthreadRunning := False;
end;
Uses
- System.Classes.TThread.Create ( fr | de | ja )
- System.Classes.TThread.Priority ( fr | de | ja )
- System.Classes.TThread.FreeOnTerminate ( fr | de | ja )
- System.Classes.TThread.Resume ( fr | de | ja )
- System.Classes.TThread.Execute ( fr | de | ja )
- System.Classes.TThreadPriority ( fr | de | ja )