请问用delphi如何实现线程池? 请问用delphi如何实现线程池?就像.net一样简单。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 直接Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf work), StObj)就可以了 Delphi也可以的。unit ThreadPoolUnit;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;type TForm3 = class(TForm) PaintBox1: TPaintBox; Button1: TButton; ListBox1: TListBox; procedure Button1Click(Sender: TObject); private type TWorkerColor = class FThreadID: Integer; FColor: TColor; FForm: TForm3; procedure PaintLines(Sender: TObject); procedure PaintLine; constructor Create(AForm: TForm3; AColor: TColor); end; var FIndex: Integer; public { Public declarations } end; TObjectHelper = class helper for TObject end; TThreadPool = class private type TUserWorkItem = class FSender: TObject; FWorkerEvent: TNotifyEvent; end; class procedure QueueWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent; Flags: ULONG); overload; static; public class procedure QueueWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent); overload; static; class procedure QueueIOWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent); static; class procedure QueueUIWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent); static; end;var Form3: TForm3; ThreadPool: TThreadPool;implementation{$R *.dfm}const WT_EXECUTEDEFAULT = ULONG($00000000); WT_EXECUTEINIOTHREAD = ULONG($00000001); WT_EXECUTEINUITHREAD = ULONG($00000002); WT_EXECUTEINWAITTHREAD = ULONG($00000004); WT_EXECUTEONLYONCE = ULONG($00000008); WT_EXECUTEINTIMERTHREAD = ULONG($00000020); WT_EXECUTELONGFUNCTION = ULONG($00000010); WT_EXECUTEINPERSISTENTIOTHREAD = ULONG($00000040); WT_EXECUTEINPERSISTENTTHREAD = ULONG($00000080); WT_TRANSFER_IMPERSONATION = ULONG($00000100);function QueueUserWorkItem (func: TThreadStartRoutine; Context: Pointer; Flags: ULONG): BOOL; stdcall; external kernel32 name 'QueueUserWorkItem';function InternalThreadFunction(lpThreadParameter: Pointer): Integer; stdcall;begin Result := 0; try try with TThreadPool.TUserWorkItem(lpThreadParameter) do if Assigned(FWorkerEvent) then FWorkerEvent(FSender); finally TThreadPool.TUserWorkItem(lpThreadParameter).Free; end; except end;end;{ TThreadPool }class procedure TThreadPool.QueueWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent);begin QueueWorkItem(Sender, WorkerEvent, WT_EXECUTEDEFAULT);end;class procedure TThreadPool.QueueIOWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent);begin QueueWorkItem(Sender, WorkerEvent, WT_EXECUTEINIOTHREAD);end;class procedure TThreadPool.QueueUIWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent);begin QueueWorkItem(Sender, WorkerEvent, WT_EXECUTEINUITHREAD);end;class procedure TThreadPool.QueueWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent; Flags: ULONG);var WorkItem: TUserWorkItem;begin if Assigned(WorkerEvent) then begin IsMultiThread := True; WorkItem := TUserWorkItem.Create; try WorkItem.FWorkerEvent := WorkerEvent; WorkItem.FSender := Sender; if not QueueUserWorkItem(InternalThreadFunction, WorkItem, Flags) then RaiseLastOSError; except WorkItem.Free; raise; end; end;end;procedure TForm3.Button1Click(Sender: TObject);begin FIndex := PaintBox1.Height; PaintBox1.Repaint; ListBox1.Items.Clear; TWorkerColor.Create(Self, clBlue); TWorkerColor.Create(Self, clRed); TWorkerColor.Create(Self, clYellow); TWorkerColor.Create(Self, clLime); TWorkerColor.Create(Self, clFuchsia); TWorkerColor.Create(Self, clTeal);end;{ TForm3.TWorkerColor }constructor TForm3.TWorkerColor.Create(AForm: TForm3; AColor: TColor);begin FForm := AForm; FColor := AColor; TThreadPool.QueueWorkItem(Self, PaintLines);end;procedure TForm3.TWorkerColor.PaintLines(Sender: TObject);var I: Integer;begin FThreadID := GetCurrentThreadID; for I := 0 to 9 do begin PaintLine; //TThread.Synchronize(nil, PaintLine); Sleep(100); end; Destroy;end;procedure TForm3.TWorkerColor.PaintLine;begin FForm.PaintBox1.Canvas.Lock; try FForm.ListBox1.Items.Add(IntToStr(FThreadID)); with FForm.PaintBox1 do begin Canvas.Pen.Color := FColor; Canvas.Polyline([Point(0, FForm.FIndex), Point(Width, FForm.FIndex)]); Dec(FForm.FIndex); if FForm.FIndex <= 0 then FForm.FIndex := 0; end; finally FForm.PaintBox1.Canvas.Unlock; end;end;end. 到CodeGear官方站点的代码中心去下载就有。 http://cc.codegear.com/Download.aspx?id=25023 在Delphi7 不能够通过编译 语法有变化 indy10的idtcpserver如何发送和接受记录类型数据? 周末也很累啊 怎样将4张分别为2.5M的BMP图片快速保存到SQLSERVER数据库? 关于在打印报表时遇到的问题? 帮我看看,怎么样才能让那个progressbar能正确反应进度 delphi6是否需要ado补丁,有这方面经验的高手进来谈谈 怎样去掉程序打开时的登录窗口? 得到电脑发出声音的音量 怎样将桌面上的图标全都隐藏起来。 难道这是Delphi的Bug吗? sql 公式 AcroPDFLib组件去哪儿找啊?
unit ThreadPoolUnit;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm3 = class(TForm)
PaintBox1: TPaintBox;
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
private
type
TWorkerColor = class
FThreadID: Integer;
FColor: TColor;
FForm: TForm3;
procedure PaintLines(Sender: TObject);
procedure PaintLine;
constructor Create(AForm: TForm3; AColor: TColor);
end;
var
FIndex: Integer;
public
{ Public declarations }
end; TObjectHelper = class helper for TObject end; TThreadPool = class
private
type
TUserWorkItem = class
FSender: TObject;
FWorkerEvent: TNotifyEvent;
end;
class procedure QueueWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent; Flags: ULONG); overload; static;
public
class procedure QueueWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent); overload; static;
class procedure QueueIOWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent); static;
class procedure QueueUIWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent); static;
end;var
Form3: TForm3;
ThreadPool: TThreadPool;implementation{$R *.dfm}const
WT_EXECUTEDEFAULT = ULONG($00000000);
WT_EXECUTEINIOTHREAD = ULONG($00000001);
WT_EXECUTEINUITHREAD = ULONG($00000002);
WT_EXECUTEINWAITTHREAD = ULONG($00000004);
WT_EXECUTEONLYONCE = ULONG($00000008);
WT_EXECUTEINTIMERTHREAD = ULONG($00000020);
WT_EXECUTELONGFUNCTION = ULONG($00000010);
WT_EXECUTEINPERSISTENTIOTHREAD = ULONG($00000040);
WT_EXECUTEINPERSISTENTTHREAD = ULONG($00000080);
WT_TRANSFER_IMPERSONATION = ULONG($00000100);function QueueUserWorkItem (func: TThreadStartRoutine; Context: Pointer; Flags: ULONG): BOOL; stdcall; external kernel32 name 'QueueUserWorkItem';function InternalThreadFunction(lpThreadParameter: Pointer): Integer; stdcall;
begin
Result := 0;
try
try
with TThreadPool.TUserWorkItem(lpThreadParameter) do
if Assigned(FWorkerEvent) then
FWorkerEvent(FSender);
finally
TThreadPool.TUserWorkItem(lpThreadParameter).Free;
end;
except end;
end;{ TThreadPool }class procedure TThreadPool.QueueWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent);
begin
QueueWorkItem(Sender, WorkerEvent, WT_EXECUTEDEFAULT);
end;class procedure TThreadPool.QueueIOWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent);
begin
QueueWorkItem(Sender, WorkerEvent, WT_EXECUTEINIOTHREAD);
end;class procedure TThreadPool.QueueUIWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent);
begin
QueueWorkItem(Sender, WorkerEvent, WT_EXECUTEINUITHREAD);
end;class procedure TThreadPool.QueueWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent; Flags: ULONG);
var
WorkItem: TUserWorkItem;
begin
if Assigned(WorkerEvent) then
begin
IsMultiThread := True;
WorkItem := TUserWorkItem.Create;
try
WorkItem.FWorkerEvent := WorkerEvent;
WorkItem.FSender := Sender;
if not QueueUserWorkItem(InternalThreadFunction, WorkItem, Flags) then
RaiseLastOSError;
except
WorkItem.Free;
raise;
end;
end;
end;procedure TForm3.Button1Click(Sender: TObject);
begin
FIndex := PaintBox1.Height;
PaintBox1.Repaint;
ListBox1.Items.Clear;
TWorkerColor.Create(Self, clBlue);
TWorkerColor.Create(Self, clRed);
TWorkerColor.Create(Self, clYellow);
TWorkerColor.Create(Self, clLime);
TWorkerColor.Create(Self, clFuchsia);
TWorkerColor.Create(Self, clTeal);
end;{ TForm3.TWorkerColor }constructor TForm3.TWorkerColor.Create(AForm: TForm3; AColor: TColor);
begin
FForm := AForm;
FColor := AColor;
TThreadPool.QueueWorkItem(Self, PaintLines);
end;procedure TForm3.TWorkerColor.PaintLines(Sender: TObject);
var
I: Integer;
begin
FThreadID := GetCurrentThreadID;
for I := 0 to 9 do
begin
PaintLine;
//TThread.Synchronize(nil, PaintLine);
Sleep(100);
end;
Destroy;
end;procedure TForm3.TWorkerColor.PaintLine;
begin
FForm.PaintBox1.Canvas.Lock;
try
FForm.ListBox1.Items.Add(IntToStr(FThreadID));
with FForm.PaintBox1 do
begin
Canvas.Pen.Color := FColor;
Canvas.Polyline([Point(0, FForm.FIndex), Point(Width, FForm.FIndex)]);
Dec(FForm.FIndex);
if FForm.FIndex <= 0 then
FForm.FIndex := 0;
end;
finally
FForm.PaintBox1.Canvas.Unlock;
end;
end;end.