贴个例子,就是通过重复执行每个线程来测试线程级别的。
////////////////////////// 程序 //////////////////////////////////
unit ThreadFrmUnit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, Buttons, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
Bevel1: TBevel;
TrackBar1: TTrackBar;
TrackBar2: TTrackBar;
Label3: TLabel;
Label4: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure TrackBar1Change(Sender: TObject);
procedure TrackBar2Change(Sender: TObject);
procedure FormShow(Sender: TObject);
private
Procedure MyThreadDown(Sender: TObject);
function SetPriority(const i:integer):TThreadPriority;
procedure SetButtonEnabled(const k:Integer);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses ThreadUnit;
{$R *.DFM}
Var
MyThread1,MyThread2,Mythread3 :TMythread;
ThreadsRunning:integer;
procedure TForm1.Button1Click(Sender: TObject);
begin
ThreadsRunning:=2;
MyThread1 :=TMyThread.Create(edit1,10000);
//建立线程1
with MyThread1do
begin
OnTerminate:=MyThreadDown;
Priority :=SetPriority(TrackBar1.Position);
//设置线程优先级别
end;
MyThread2 :=TMyThread.Create(edit2,10000);
//建立线程2
with MyThread2do
begin
OnTerminate:=MyThreadDown;
Priority :=SetPriority(TrackBar2.Position);
//设置线程优先级别
end;
SetButtonEnabled(0);
//设置按钮状态
end;
function TForm1.SetPriority(const i:integer):TThreadPriority;
//线程优先级别
begin
Result :=tpIdle;
Case i of
0: Result:=tpIdle;
1: Result:=tpLowest;
2: Result:=tpLower;
3: Result:=tpNormal;
4: Result:=tpHigher;
5: Result:=tpHighest;
6: Result:=tpTimeCritical;
end;
//case
end;
Procedure TForm1.MyThreadDown(Sender: TObject);
begin
dec(ThreadsRunning);
//线程减一
if ThreadsRunning=0 then
Form1.Button1.Enabled:=True;
end;
procedure TForm1.TrackBar1Change(Sender: TObject);
begin
Label1.caption :='Thread1优先级别:'+IntToStr(TrackBar1.Position);
end;
procedure TForm1.TrackBar2Change(Sender: TObject);
begin
Label2.caption :='Thread2优先级别:'+IntToStr(TrackBar2.Position);
end;
Procedure Tform1.SetButtonEnabled(const k:integer);
//设置按钮状态
begin
if k=0 then
begin
button1.Enabled:=false;
Button2.enabled:=true;
Button3.enabled:=true;
// Button4.enabled:=True;
end else
begin
button1.Enabled:=true;
Button2.enabled:=False;
Button3.enabled:=false;
Button4.enabled:=false;
end;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
SetButtonEnabled(1);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
MyThread1.Suspended :=True;
Button3.Enabled:=false;
Button4.Enabled:=True;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
MyThread1.Suspended :=False;
Button3.Enabled :=True;
Button4.Enabled:=false;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if MyThread1<>nil then
MyThread1.Terminate;
if MyThread2<>nil then
MyThread2.Terminate;
SetButtonEnabled(1);
end;
end.
////////////////////////// 引用的线程单元 ////////////////////////
unit ThreadUnit;
interface
uses
Classes,SysUtils,StdCtrls;
type
TMyThread = class(TThread)
private
{ Private declarations }
Aedit :TEdit;
MaxLoop,CurrentLoop:Integer;
protected
procedure Execute;
override;
procedure DispLoop;
Public
constructor Create(Edit:TEdit;
Max:integer);
end;
implementation
constructor TMyThread.Create(Edit:TEdit;Max:integer);
begin
Inherited Create(False);
AEdit :=Edit;
MaxLoop:=Max;
FreeOnTerminate:=True;
end;
procedure TMyThread.Execute;
var i:integer;
begin
for i:=0 to MaxLoopdo
begin
CurrentLoop:=i;
Synchronize(DispLoop);
if Terminated then
Exit;
end;
end;
procedure TMyThread.DispLoop;
begin
AEdit.text:=IntToStr(CurrentLoop);
end;
end.