Delphi线程类 DIY,把类指针作为参数传进去,就可以执行类里面的方法啦

Delphi 封装了一个很强大的线程类 TThread,

我们也自己动手制作一个简单的线程类

首先Type一个类

[delphi]view plaincopy

  1. type
  2. TwwThread = class
  3. constructor Create; overload;
  4. destructor Destroy; override;
  5. private
  6. m_hThread: THandle; //线程
  7. m_ThreadID : TThreadID;
  8. public
  9. procedure Execute;
  10. end;

[delphi]view plaincopy

  1. function wwThreadProc(Thread: TwwThread): Integer;
  2. begin
  3. Thread.Execute(); //
  4. end;
  5. constructor TwwThread.Create;
  6. begin
  7. m_hThread := CreateThread(nil, 0, @wwThreadProc, Pointer(Self), 0, m_ThreadID); { 这里的重点是第四个参数, 把类指针作为参数传进去,这样就可以执行类里面的方法啦}
  8. end;
  9. destructor TwwThread.Destroy;
  10. begin
  11. inherited;
  12. end;
  13. procedure TwwThread.Execute;
  14. begin
  15. // 这里写上代码
  16. end;

声明一下:

[delphi]view plaincopy

  1. var
  2. myThread : TwwThread;

开始~

[delphi]view plaincopy

    1. myThread := TwwThread.Create;

http://blog.csdn.net/warrially/article/details/7887700