TThreadPriority (C++)

From RAD Studio Code Examples
Jump to: navigation, search

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

bool mythreadRunning;
bool youthreadRunning;

class TMyThread : public TThread
{
__published:	 
private:	 
protected:   
	void __fastcall Execute();
public:		// User declarations
	__fastcall TMyThread(bool suspended);
};

__fastcall TMyThread::TMyThread(bool suspended) : TThread(suspended)
{
}

class TYouThread : public TThread
{
__published:
private:
protected:
	void __fastcall Execute();
public:
	__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; 
  if (mythreadRunning == FALSE)
  {
	mythreadRunning = TRUE;
	SecondProcess = new TMyThread(True); // Creation suspended--the second process does not run yet.
	SecondProcess->FreeOnTerminate = True; // No need to clean up after termination
	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); // Creation suspended--the second process does not run yet.
	SecondProcess->FreeOnTerminate = True; // No need to clean up after termination
	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);
}

__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
  mythreadRunning = FALSE;
  youthreadRunning = FALSE;
}

Uses