RAD Studio VCL Reference
|
Provides an abstract or pure virtual method to contain the code which executes when the thread is run.
procedure Execute; virtual; abstract;
virtual __fastcall Execute() = 0;
Override Execute and insert the code that should be executed when the thread runs. Execute is responsible for checking the value of the Terminated property to determine if the thread needs to exit.
A thread executes when Create is called if CreateSuspended set to false, or when Resume is first called after the thread is created if CreateSuspended set to true.
C++ Examples:
/* 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 is True, so that there is no need to free the thread when it finishes. To create the extension choose the File | New | Other | Thread Object menu. */ bool mythreadRunning; bool youthreadRunning; class TMyThread : public TThread { __published: // IDE-managed Components private: // User declarations protected: // User declarations void __fastcall Execute(); public: // User declarations __fastcall TMyThread(bool suspended); }; __fastcall TMyThread::TMyThread(bool suspended) : TThread(suspended) { } class TYouThread : public TThread { __published: // IDE-managed Components private: // User declarations protected: // User declarations void __fastcall Execute(); public: // User declarations __fastcall TYouThread(bool suspended); }; __fastcall TYouThread::TYouThread(bool suspended) : TThread(suspended) { } void __fastcall TMyThread::Execute() { Form1->Memo1->Lines->Add("Process has been running for this many seconds:"); for (int I = 0; I <= 10; I++) { Form1->Memo1->Lines->Add("Low priority process " + IntToStr(I)); Sleep(1000); } mythreadRunning = FALSE; } void __fastcall TYouThread::Execute() { Form1->Memo3->Lines->Add("Second low priority process has been running for this many seconds:"); for (int I = 0; I <= 10; I++) { Form1->Memo3->Lines->Add("Second low priority process " + IntToStr(I)); Sleep(1000); } youthreadRunning = FALSE; } void __fastcall TForm1::Button1Click(TObject *Sender) { TMyThread *SecondProcess; // TMyThread is a custom descendant of TThread if (mythreadRunning == FALSE) { mythreadRunning = TRUE; SecondProcess = new TMyThread(True); // create suspended – secondprocess does not run yet SecondProcess->FreeOnTerminate = True; // don't need to cleanup after terminate SecondProcess->Priority = tpLower; // set the priority to lower than normal SecondProcess->Resume(); // now run the thread } else MessageDlg("This thread is still running. You are going to hurt yourself!", mtInformation, TMsgDlgButtons() << mbOK, 0); } void __fastcall TForm1::Button2Click(TObject *Sender) { Form1->Memo2->Lines->Add("Do some work in the main process for 10 seconds:"); for (int I = 0; I <= 10; I++) { Form1->Memo2->Lines->Add("Main process " + IntToStr(I)); Sleep(1000); } } void __fastcall TForm1::Button3Click(TObject *Sender) { TYouThread *SecondProcess; // TMyThread is a custom descendant of TThread if (youthreadRunning == FALSE) { youthreadRunning = TRUE; SecondProcess = new TYouThread(True); // create suspended – secondprocess does not run yet SecondProcess->FreeOnTerminate = True; // don't need to cleanup after terminate SecondProcess->Priority = tpLower; // set the priority to lower than normal SecondProcess->Resume(); // now run the thread } else MessageDlg("This thread is still running. You are going to hurt yourself!", mtInformation, TMsgDlgButtons() << mbOK, 0); } void __fastcall TForm1::FormCreate(TObject *Sender) { mythreadRunning = FALSE; youthreadRunning = FALSE; }
Delphi Examples:
{ 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 is 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 an "Abstract Error" will result. To create the extension choose the File | New | Other | Thread Object menu. } procedure TForm1.Button1Click(Sender: TObject); begin if (mythreadRunning = FALSE) then begin mythreadRunning:= TRUE; MyProcess := TMyThread.Create(True); { create suspended - secondprocess does not run yet } MyProcess.FreeOnTerminate := True; { don't need to cleanup after terminate } 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 – secondprocess does not run yet } YouProcess.FreeOnTerminate := True; { don't need to cleanup after terminate } 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;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|