博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于delphi中多线程采用定时器的做法
阅读量:7026 次
发布时间:2019-06-28

本文共 2371 字,大约阅读时间需要 7 分钟。

delphi中的提供了一个tthread的多线程类,开发者可以根据自身的需要,定制相应功能的多线程,而定时器在多线程中有很广泛的应用,在这里,只说关于waitforsingleobject来做定时器的一些关键问题。(关于定时器的相关知识,请阅读《深度历险》)

waitforsingleobject是一个api函数,采用该函数,需要调用createevent,timesetevent,然后才能使用。但是这里使用到的event在线程中声明的位置不一样,效果也不一样,以下给出两种声明方式:
unit PTUnit;
interface
uses
  Classes,mmsystem, Windows, sysutils, Forms;
type
  TPingThread = class(TThread)
  private
    { Private declarations }
   {将event声明为该线程类的私有数据成员}
    timerid:integer;
    htimerevent:Thandle;
    fFileName: string;
  protected
    procedure Execute; override;
  public
    constructor create;
    procedure SetOver;
  published
  end;
implementation
constructor TPingThread.Create;
begin
  FreeOnTerminate := true;
  Inherited Create(true);
end;
procedure TPingThread.SetOver;
begin
  timerid := timesetevent(5,0,TFNTimecallback(htimerevent),0,time_periodic or time_callback_event_set);
end;
procedure TPingThread.Execute;
begin
  htimerevent := CreateEvent(nil, False, False, nil);
  timerid := timesetevent(5*1000,0,TFNTimecallback(htimerevent),0,time_periodic or time_callback_event_set);
  repeat
    if WaitForSingleObject(htimerevent,INFINITE) = WAIT_OBJECT_0 then
    begin
      if Terminated then break;
      dosomething;
   end;
  until false;
  timekillevent(timerid);
  CloseHandle(htimerevent);
end;
end.
这种声明方式保证了线程能够正常的执行。但是,下面一种声明方式就不行了,当你只是创建一个线程实例的时候,还能正常的执行,如果创建了两个以上的线程实例,那就不对了。
unit PTUnit;
interface
uses
  Classes,mmsystem, Windows, sysutils, Forms;
type
  TPingThread = class(TThread)
  private
    { Private declarations }
    fFileName: string;
  protected
    procedure Execute; override;
  public
    constructor create;
    procedure SetOver;
  published
  end;
implementation
{在实现部分声明事件}
var
  timerid:integer;
  htimerevent:Thandle;
constructor TPingThread.Create;
begin
  FreeOnTerminate := true;
  Inherited Create(true);
end;
procedure TPingThread.SetOver;
begin
  timerid := timesetevent(5,0,TFNTimecallback(htimerevent),0,time_periodic or time_callback_event_set);
end;
procedure TPingThread.Execute;
begin
  htimerevent := CreateEvent(nil, False, False, nil);
  timerid := timesetevent(5*1000,0,TFNTimecallback(htimerevent),0,time_periodic or time_callback_event_set);
  repeat
    if WaitForSingleObject(htimerevent,INFINITE) = WAIT_OBJECT_0 then
    begin
      if Terminated then break;
      dosomething;
   end;
  until false;
  timekillevent(timerid);
  CloseHandle(htimerevent);
end;
end.
以上两个除了事件声明位置不一样,其他功能均一样的代码在创建了两个以上线程实例的时候,第一个能够正常执行,第二个中的定时器的时间间隔就会出问题,而且当结束其中一个线程的时候,另外一个线程也无法正常工作。
总结:建议使用第一种声明方式,这种方式能确保正常运行。

转载地址:http://jloxl.baihongyu.com/

你可能感兴趣的文章
传统短视频直播平台和新兴一对一交友源码力与美的结合
查看>>
撩课大前端-面试宝典-第七篇
查看>>
开源大数据周刊-第3期
查看>>
java版 b2b2c o2o电子商务云商平台spring cloud+springmvc+mybatis
查看>>
区块链100讲:Hyperledger Fabric 区块链多机部署
查看>>
重学前端学习笔记(十九)--JavaScript中的函数
查看>>
SpringBoot2.1版本的个人应用开发框架 - 整合vue实现前后端分离
查看>>
Rxjava2源码分析之线程切换(subscribeOn、observeOn)
查看>>
SpringBoot整合Mybatis
查看>>
KNN分类器-Java实现
查看>>
从事iOS研发6年的面经——希望对你们有帮助,程序员必看!
查看>>
uni-app 打开第三方程序
查看>>
fastlane 的集成与使用
查看>>
Android学习之JSON解析(二)使用GSON技术解析JSON
查看>>
JavaScript中的原型链和继承
查看>>
Java集合类的使用
查看>>
Axios源码分析
查看>>
RAC的函数式编程
查看>>
WEB安全知多少
查看>>
scheduleWithFixedDelay和scheduleAtFixedRate源码分析
查看>>