自己封装线程(Demo)
生活随笔
收集整理的這篇文章主要介紹了
自己封装线程(Demo)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼 unit?theadTest;
interface
uses
??Windows,?Classes;
type
??MyThread?=?class
??private
????FHandle:?THandle;
????FSuspended:?Boolean;
????FThreadID:?THandle;
????FThradNodity:?TNotifyEvent;
????FTerminated:??Boolean;
????procedure?SetSuspended(Value:?Boolean);
??protected
????procedure?Execute;?virtual;
????property?Terminated:?Boolean?read?FTerminated;
??public
????constructor?Create(CreateSuspended:?Boolean);
????destructor?Destroy;?override;
????procedure?Terminate;
????property?Handle:?THandle?read?FHandle;
????property?Suspended:?Boolean?read?FSuspended?write?SetSuspended;
????property?ThreadID:?THandle?read?FThreadID;
????property?ThradNodity:?TNotifyEvent?read?FThradNodity?write?FThradNodity;
??end;
implementation
{?MyThread?}
function?ThreadProc(Param:?Pointer):?Integer;
begin
??try
????if?not?MyThread(Param).Terminated?then
??????try
????????MyThread(Param).Execute;
??????except
??????end;
??finally
????Result?:=?0;
??end;
end;??
constructor?MyThread.Create(CreateSuspended:?Boolean);
begin
??inherited?Create;
??FSuspended?:=?CreateSuspended;
??FHandle?:=?BeginThread(nil,?0,?@ThreadProc,?Pointer(Self),?CREATE_SUSPENDED,?FThreadID);
end;
destructor?MyThread.Destroy;
begin
??if?FHandle?<>?0?then?CloseHandle(FHandle);
??inherited?Destroy;
end;
procedure?MyThread.Execute;
begin
??while?not?Terminated?do
??begin
????if?Assigned(ThradNodity)?then
??????ThradNodity(nil);
??end;??
end;
procedure?MyThread.SetSuspended(Value:?Boolean);
begin
??if?Value?<>?FSuspended?then
????FSuspended?:=?Value;
end;
procedure?MyThread.Terminate;
begin
??FTerminated?:=?True;
end;
end.
調用
var
??Test1:?MyThread;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
? if Assigned(Test1) then
? begin
??? if Test1.Handle <> 0 then
????? Test1.Terminate;
??? Test1.Free;
? end;
end;
思路基本是DELPHI?VCL?中Thread的封裝思路,主要是測試用 VCL 中先創建一個掛起的線程,然后再調用,調用過程還是有點小復雜,
interface
uses
??Windows,?Classes;
type
??MyThread?=?class
??private
????FHandle:?THandle;
????FSuspended:?Boolean;
????FThreadID:?THandle;
????FThradNodity:?TNotifyEvent;
????FTerminated:??Boolean;
????procedure?SetSuspended(Value:?Boolean);
??protected
????procedure?Execute;?virtual;
????property?Terminated:?Boolean?read?FTerminated;
??public
????constructor?Create(CreateSuspended:?Boolean);
????destructor?Destroy;?override;
????procedure?Terminate;
????property?Handle:?THandle?read?FHandle;
????property?Suspended:?Boolean?read?FSuspended?write?SetSuspended;
????property?ThreadID:?THandle?read?FThreadID;
????property?ThradNodity:?TNotifyEvent?read?FThradNodity?write?FThradNodity;
??end;
implementation
{?MyThread?}
function?ThreadProc(Param:?Pointer):?Integer;
begin
??try
????if?not?MyThread(Param).Terminated?then
??????try
????????MyThread(Param).Execute;
??????except
??????end;
??finally
????Result?:=?0;
??end;
end;??
constructor?MyThread.Create(CreateSuspended:?Boolean);
begin
??inherited?Create;
??FSuspended?:=?CreateSuspended;
??FHandle?:=?BeginThread(nil,?0,?@ThreadProc,?Pointer(Self),?CREATE_SUSPENDED,?FThreadID);
end;
destructor?MyThread.Destroy;
begin
??if?FHandle?<>?0?then?CloseHandle(FHandle);
??inherited?Destroy;
end;
procedure?MyThread.Execute;
begin
??while?not?Terminated?do
??begin
????if?Assigned(ThradNodity)?then
??????ThradNodity(nil);
??end;??
end;
procedure?MyThread.SetSuspended(Value:?Boolean);
begin
??if?Value?<>?FSuspended?then
????FSuspended?:=?Value;
end;
procedure?MyThread.Terminate;
begin
??FTerminated?:=?True;
end;
end.
調用
var
??Test1:?MyThread;
var
? H: THandle;
begin
? Test1 := MyThread.Create(False);
? Test1.ThradNodity := ThreadNodity;
? DuplicateHandle(GetCurrentProcess, Test1.Handle, GetCurrentProcess, @H, DUPLICATE_SAME_ACCESS, TRUE, DUPLICATE_SAME_ACCESS);
? Windows.ResumeThread(H);
//? Windows.ResumeThread(Test1.Handle);
? Windows.ResumeThread(H);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
? if Assigned(Test1) then
? begin
??? if Test1.Handle <> 0 then
????? Test1.Terminate;
??? Test1.Free;
? end;
end;
思路基本是DELPHI?VCL?中Thread的封裝思路,主要是測試用 VCL 中先創建一個掛起的線程,然后再調用,調用過程還是有點小復雜,
?
轉載于:https://www.cnblogs.com/chengxin1982/archive/2009/12/14/1623575.html
總結
以上是生活随笔為你收集整理的自己封装线程(Demo)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Delphi的内存管理及内存泄露问题
- 下一篇: 无法创建Web Application项