zoukankan      html  css  js  c++  java
  • 自己封装线程(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(nil0, @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 中先创建一个挂起的线程,然后再调用,调用过程还是有点小复杂,
  • 相关阅读:
    nyoj 329 循环小数【KMP】【求最小循环节长度+循环次数+循环体】
    转 :hdoj 4857 逃生【反向拓扑】
    hdoj 3342 Legal or Not【拓扑排序】
    hdoj 2094 产生冠军
    poj 1789 Truck History【最小生成树prime】
    转:【拓扑排序详解】+【模板】
    hdoj 1285 确定比赛名次【拓扑排序】
    poj 2031 Building a Space Station【最小生成树prime】【模板题】
    zzuoj 10408: C.最少换乘【最短路dijkstra】
    [LC] 232. Implement Queue using Stacks
  • 原文地址:https://www.cnblogs.com/chengxin1982/p/1623575.html
Copyright © 2011-2022 走看看