TThreadYield (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example shows how to use the Yield method, the Priority property and several others listed in the Uses clause. This example demonstrates how to use the Synchronize method to interact with VCL components in a process intensive, high priority situation. Warning: running at these high priorities demonstrates the effect of the Yield method, but also will lock out button clicks and other Windows processes.


To use the sample you need to add five TButtons on the form, four used to start and terminate two threads and one used to call Yield method. Also the sample uses two TMemo components to display the status of the threads and a TSpinEdit to set the amount of time to work fot every thread.

Code

// From TThreadYield.h:

class TMyThread : public TThread
{
	private:
	double FTime;
	String memoline1, memoline2;
	 void __fastcall MemoAdd1();
	 void __fastcall MemoAdd2();
	protected:
		void __fastcall Execute();

	public:
	__fastcall TMyThread();
	void __fastcall OnTerminate(TObject *Sender);

};

// From TThreadYield.cpp:

__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) {
	seTimeToWork->Value = 30000;
}

__fastcall TMyThread::TMyThread() : TThread() {
	FTime = Form1->seTimeToWork->Value;
}

void __fastcall TMyThread::MemoAdd1() {
	Form1->Memo1->Lines->Add(this->memoline1);
}

void __fastcall TMyThread::MemoAdd2() {
	Form1->Memo2->Lines->Add(this->memoline2);
}

void __fastcall TMyThread::Execute() {
	int mysleep, supersleep;
	double T;
	T = FTime;
	memoline2 = "Begin execution";
	Synchronize(MemoAdd1);

	if (IsSingleProcessor) {
		memoline1 = "The system has one processor";
		Synchronize(MemoAdd1);
	}
	else {
		memoline1 = "The system has " + IntToStr(ProcessorCount) +
			" processors";
		Synchronize(MemoAdd1);
	};
	do {
		mysleep = 100000000;
		memoline2 =
		  Format("Thread "+IntToStr((int)ThreadID) + ":  %5.2f%%", ARRAYOFCONST((100.0 - T / FTime * 100.0)));
		Synchronize(MemoAdd2);
		while (mysleep > 0) {
			supersleep = mysleep * mysleep;
			mysleep = mysleep - 1;
		};
		T = T - 500;
	}
	while (!CheckTerminated() && (T >= 0));
	if (Terminated) {
		memoline1 = "Terminated by user";
		Synchronize(MemoAdd1);
	}
	memoline1 = "Finish execution";
	Synchronize(MemoAdd1);
	memoline2 = "Terminated by user";
	Synchronize(MemoAdd2);
	memoline2 = "Finish execution";
	Synchronize(MemoAdd2);
}

void __fastcall TMyThread::OnTerminate(TObject * Sender) {
	FreeAndNil(this);
}

// ---------------------------------------------------------------------------
void __fastcall TForm1::BtnCreateClick(TObject * Sender) {
	FThread = new TMyThread();
	FThread->Priority = tpHigher;
	FThread->FreeOnTerminate = False;
	// Free FThread in the OnTerminate proc. This will set Terminated to true.
	FThread->Resume();
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::BtnTerminateClick(TObject * Sender) {
	if (FThread != NULL) {
		FThread->Terminate();
		Form1->Memo1->Lines->Add("Start terminating FThread.");
			// Calling Free on a TThread will set Terminated := True
			// and then block until the thread terminates.
		}
	else {
		Form1->Memo1->Lines->Add("Thread does not exist");
	}
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::BtnYieldClick(TObject * Sender) {
	if (FThread != NULL) {
		FThread->Yield();
		Form1->Memo1->Lines->Add("Thread yielded");
	}
	else
		Form1->Memo1->Lines->Add("Thread1 does not exist");
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::BtnCreate1Click(TObject * Sender) {
	FThread1 = new TMyThread();
	FThread1->Priority = tpNormal;
	FThread1->FreeOnTerminate = False;
	// Free FThread in the OnTerminate proc. This will set Terminated to true.
	FThread1->Resume();
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::BtnTerminate1Click(TObject * Sender) {

	if (FThread1 != NULL) {

		FThread1->Terminate();
		Form1->Memo1->Lines->Add("Start terminating FThread1.");
			// Calling Free on a TThread will set Terminated := True
			// and then block until the thread terminates.
		}else {
		Form1->Memo1->Lines->Add("Thread1 does not exist");
	}
}

Uses