你真可以啊。我不知道是帮你还是害你,但希望对你有帮助。下面是我写的DEMO:
两个单元:Unit1和Unit2。UNIT1主要是界面,UNIT2则是线程和任务LIST实现。
//*****************Unit1.pas********************
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, CheckLst, Unit2, ExtCtrls;
type
TForm1 = class(TForm)
CheckListBoxTasks: TCheckListBox;
Label1: TLabel;
ButtonAddRandomTask: TButton;
ButtonStopCurTask: TButton;
ButtonPauseTask: TButton;
ButtonExit: TButton;
ButtonAddTask: TButton;
EditTaskTime: TEdit;
TimerRefreshList: TTimer;
ButtonDelTask: TButton;
ButtonContinueTask: TButton;
procedure ButtonExitClick(Sender: TObject);
procedure ButtonAddTaskClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure TimerRefreshListTimer(Sender: TObject);
procedure ButtonAddRandomTaskClick(Sender: TObject);
procedure ButtonDelTaskClick(Sender: TObject);
procedure ButtonStopCurTaskClick(Sender: TObject);
procedure ButtonPauseTaskClick(Sender: TObject);
procedure ButtonContinueTaskClick(Sender: TObject);
private
{ Private declarations }
FTaskList: TTaskList;
FTaskIdx: Integer;
FThreads: array of TTaskThread;
public
{ Public declarations }
end;
var
Form1: TForm1;
const
THREAD_COUNT = 4;
implementation
{$R *.dfm}
procedure TForm1.ButtonExitClick(Sender: TObject);
begin
Close;
end;
procedure TForm1.ButtonAddTaskClick(Sender: TObject);
begin
Inc(FTaskIdx);
FTaskList.AddTask(Format('Task%d', [FTaskIdx]),
StrToIntDef(EditTaskTime.Text, 1000));
end;
procedure TForm1.FormCreate(Sender: TObject);
var
I: Integer;
begin
FTaskList := TTaskList.Create;
FTaskIdx := 0;
Randomize;
SetLength(FThreads, THREAD_COUNT);
for I := 0 to High(FThreads)do
FThreads := TTaskThread.Create(FTaskList);
end;
procedure TForm1.FormDestroy(Sender: TObject);
var
I, J: Integer;
begin
for I := 0 to High(FThreads)do
FThreads.Terminate;
for I := 0 to High(FThreads)do
while FThreads.Suspendeddo
FThreads.Resume;
for I := 0 to High(FThreads)do
begin
//等待线程退出
while WaitForSingleObject(FThreads.Handle, 0) <> WAIT_OBJECT_0do
for J := 0 to 1000do
Application.ProcessMessages;
end;
FTaskList.Free;
end;
procedure TForm1.TimerRefreshListTimer(Sender: TObject);
var
AList: TList;
Item: PTaskRecord;
I, C: Integer;
S: string;
begin
AList := FTaskList.LockList;
try
C := CheckListBoxTasks.Count;
if CheckListBoxTasks.Count < AList.Count then
for I := AList.Count - 1do
wnto CheckListBoxTasks.Countdo
begin
Item := AList.Items;
S := Format('%s %d %s %d%%', [Item.Name, Item.Time,
TTaskStatusNames[Item.Status], Round(Item.DoneTimes / Item.Time * 100)]);
CheckListBoxTasks.Items.Add(S);
end;
if CheckListBoxTasks.Count > AList.Count then
begin
C := AList.Count;
for I := CheckListBoxTasks.Count - 1do
wnto AList.Count - 1do
CheckListBoxTasks.Items.Delete(I);
end;
for I := C - 1do
wnto 0do
begin
Item := AList.Items;
S := Format('%s %d %s %d%%', [Item.Name, Item.Time,
TTaskStatusNames[Item.Status], Round(Item.DoneTimes / Item.Time * 100)]);
if CheckListBoxTasks.Items.Strings <> S then
CheckListBoxTasks.Items.Strings := S;
end;
finally
FTaskList.UnlockList;
end;
for I := 0 to High(FThreads)do
if FThreads.Suspended then
FThreads.CheckTask;
end;
procedure TForm1.ButtonAddRandomTaskClick(Sender: TObject);
begin
Inc(FTaskIdx);
FTaskList.AddTask(Format('Task%d', [FTaskIdx]),
Random(3000) + 1000);
end;
procedure TForm1.ButtonDelTaskClick(Sender: TObject);
var
ID: Integer;
begin
if CheckListBoxTasks.ItemIndex >= 0 then
begin
ID := CheckListBoxTasks.ItemIndex;
if FTaskList.Items[ID].Status <> tsRunning then
CheckListBoxTasks.DeleteSelected;
FTaskList.DelTask(ID);
if CheckListBoxTasks.Count > ID then
CheckListBoxTasks.ItemIndex := ID;
end;
end;
procedure TForm1.ButtonStopCurTaskClick(Sender: TObject);
begin
if CheckListBoxTasks.ItemIndex >= 0 then
FTaskList.TaskCommand(CheckListBoxTasks.ItemIndex, tcStop);
end;
procedure TForm1.ButtonPauseTaskClick(Sender: TObject);
begin
if CheckListBoxTasks.ItemIndex >= 0 then
FTaskList.TaskCommand(CheckListBoxTasks.ItemIndex, tcPause);
end;
procedure TForm1.ButtonContinueTaskClick(Sender: TObject);
begin
if CheckListBoxTasks.ItemIndex >= 0 then
FTaskList.TaskCommand(CheckListBoxTasks.ItemIndex, tcRun);
end;
end.
//*****************Unit1.dfm********************
object Form1: TForm1
Left = 295
Top = 107
Width = 458
Height = 311
Caption = 'TaskThread Demo'
Color = clBtnFace
Font.Charset = GB2312_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = '宋体'
Font.Style = []
OldCreateOrder = False
Position = poDefaultPosOnly
Scaled = False
OnCreate = FormCreate
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 12
object Label1: TLabel
Left = 16
Top = 16
Width = 60
Height = 12
Caption = '任务列表:'
end
object CheckListBoxTasks: TCheckListBox
Left = 16
Top = 36
Width = 185
Height = 233
ItemHeight = 12
TabOrder = 0
end
object ButtonAddRandomTask: TButton
Left = 244
Top = 64
Width = 121
Height = 25
Caption = '增加任务(随机延时)'
TabOrder = 1
OnClick = ButtonAddRandomTaskClick
end
object ButtonStopCurTask: TButton
Left = 244
Top = 104
Width = 121
Height = 25
Caption = '中止当前任务'
TabOrder = 2
OnClick = ButtonStopCurTaskClick
end
object ButtonPauseTask: TButton
Left = 244
Top = 136
Width = 121
Height = 25
Caption = '暂停任务'
TabOrder = 3
OnClick = ButtonPauseTaskClick
end
object ButtonExit: TButton
Left = 248
Top = 224
Width = 121
Height = 25
Caption = '退出'
TabOrder = 4
OnClick = ButtonExitClick
end
object ButtonAddTask: TButton
Left = 244
Top = 36
Width = 121
Height = 25
Caption = '增加任务'
TabOrder = 5
OnClick = ButtonAddTaskClick
end
object EditTaskTime: TEdit
Left = 208
Top = 40
Width = 33
Height = 20
TabOrder = 6
Text = '3000'
end
object ButtonDelTask: TButton
Left = 244
Top = 192
Width = 121
Height = 25
Caption = '删除任务'
TabOrder = 7
OnClick = ButtonDelTaskClick
end
object ButtonContinueTask: TButton
Left = 244
Top = 164
Width = 121
Height = 25
Caption = '继续任务'
TabOrder = 8
OnClick = ButtonContinueTaskClick
end
object TimerRefreshList: TTimer
Interval = 200
OnTimer = TimerRefreshListTimer
Left = 208
Top = 96
end
end
//*****************Unit2.pas********************
{
*****************************************************************
TaskThread
多线程任务处理 DEMO
(for fullstrong)
Date: 2001-12-14
By HUZZZ (huz0123@21cn.com)
Guangzhou China.
*****************************************************************
}
unit Unit2;
interface
uses
Classes, Windows;
type
//状态:未知,等待,运行,暂停,完成
TTaskStatus = (tsUnknow, tsWaiting, tsRunning, tsPause, tsFinished);
//命令:无,运行,暂停,停止
TTaskCommandd = (tcNone, tcRun, tcPause, tcStop);
TTaskRecord = record
Name: string;
Time: Integer;
Command: TTaskCommandd;
Status: TTaskStatus;
do
neTimes: Integer;
end;
PTaskRecord = ^TTaskRecord;
TTaskList = class(TThreadList)
private
function GetItems(Index: Integer): TTaskRecord;
procedure SetItems(Index: Integer;
const Value: TTaskRecord);
{ Private declarations }
protected
public
destructor Destroy;
override;
procedure AddTask(AName: string;
ATime: Integer);
procedure DelTask(Index: Integer);
procedure TaskCommand(Index: Integer;
Command: TTaskCommandd);
property Items[Index: Integer]: TTaskRecord read GetItems write SetItems;
end;
TTaskThread = class(TThread)
private
FTaskList: TTaskList;
FCurTask: PTaskRecord;
FRunTimes: Integer;
function GetTaskFinished: Boolean;
{ Private declarations }
protected
procedure Execute;
override;
public
procedure CheckTask;
constructor Create(TaskList: TTaskList);
property TaskFinished: Boolean read GetTaskFinished;
end;
const
TTaskStatusNames: array[TTaskStatus] of string =
('未知', '等待', '运行', '暂停', '完成');
implementation
{ TTaskList }
procedure TTaskList.AddTask(AName: string;
ATime: Integer);
var
AList: TList;
Item: PTaskRecord;
begin
AList := LockList;
try
Item := New(PTaskRecord);
Item.Name := AName;
Item.Time := ATime;
Item.DoneTimes := 0;
Item.Command := tcNone;
Item.Status := tsWaiting;
AList.Add(Item);
finally
UnlockList;
end;
end;
procedure TTaskList.DelTask(Index: Integer);
var
AList: TList;
Item: PTaskRecord;
begin
AList := LockList;
try
Item := AList.Items[Index];
if Item.Status = tsRunning then
begin
Item.Command := tcPause;
Exit;
end;
AList.Remove(Item);
Dispose(Item);
finally
UnlockList;
end;
end;
destructor TTaskList.Destroy;
var
AList: TList;
Item: PTaskRecord;
I: Integer;
begin
AList := LockList;
try
for I := AList.Count - 1do
wnto 0do
begin
Item := AList.Items;
//AList.Remove(Item);
Dispose(Item);
end;
finally
UnlockList;
end;
inherited;
end;
function TTaskList.GetItems(Index: Integer): TTaskRecord;
var
AList: TList;
begin
AList := LockList;
try
Result := TTaskRecord(AList.Items[Index]^);
finally
UnlockList;
end;
end;
procedure TTaskList.SetItems(Index: Integer;
const Value: TTaskRecord);
var
AList: TList;
begin
AList := LockList;
try
TTaskRecord(AList.Items[Index]^) := Value;
finally
UnlockList;
end;
end;
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TTaskThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
procedure TTaskList.TaskCommand(Index: Integer;
Command: TTaskCommandd);
var
AList: TList;
Item: PTaskRecord;
begin
AList := LockList;
try
Item := AList.Items[Index];
Item.Command := Command;
finally
UnlockList;
end;
end;
{ TTaskThread }
procedure TTaskThread.CheckTask;
var
AList: TList;
I: Integer;
ATask: PTaskRecord;
begin
if Assigned(FCurTask) then
begin
if FRunTimes >= FCurTask.Time then
begin
FTaskList.LockList;
try
FCurTask.Status := tsFinished;
FCurTask.DoneTimes := FRunTimes;
finally
FTaskList.UnlockList;
end;
FCurTask := nil;
end
else
if FCurTask.Command = tcStop then
begin
FTaskList.LockList;
try
FCurTask.Status := tsWaiting;
finally
FTaskList.UnlockList;
end;
FCurTask := nil;
end
else
if FCurTask.Command = tcPause then
begin
FTaskList.LockList;
try
FCurTask.Status := tsPause;
finally
FTaskList.UnlockList;
end;
FCurTask := nil;
end;
end;
if not Assigned(FCurTask) then
begin
FRunTimes := 0;
AList := FTaskList.LockList;
try
for I := 0 to AList.Count - 1do
begin
ATask := AList.Items;
case ATask.Status of
tsWaiting:
begin
FCurTask := ATask;
FCurTask.Status := tsRunning;
end;
tsPause:
if ATask.Command = tcRun then
begin
FCurTask := ATask;
FRunTimes := ATask.DoneTimes;
FCurTask.Command := tcNone;
FCurTask.Status := tsRunning;
end;
end;
if Assigned(FCurTask) then
Break;
end;
finally
FTaskList.UnlockList;
end;
end;
if Assigned(FCurTask) then
while Suspendeddo
Resume;
end;
constructor TTaskThread.Create(TaskList: TTaskList);
begin
inherited Create(True);
FTaskList := TaskList;
end;
procedure TTaskThread.Execute;
begin
{ Place thread code here }
while not Terminateddo
begin
CheckTask;
while not TaskFinished and not Terminateddo
begin
Inc(FRunTimes);
FTaskList.LockList;
try
FCurTask.DoneTimes := FRunTimes;
finally
FTaskList.UnlockList;
end;
Sleep(10);
end;
Suspend;
end;
end;
function TTaskThread.GetTaskFinished: Boolean;
begin
CheckTask;
Result := not Assigned(FCurTask);
end;
end.