多線程編程(17)

來源:互聯網
上載者:User

根據 WaitableTimer 的主要功用, 現在再把它放在 "線程同步" 的話題中討論有點不合適了, 就要結束它.

//重新看看那個 APC 回呼函數的格式:
procedure TimerAPCProc(
 lpArgToCompletionRoutine: Pointer;
 dwTimerLowValue, dwTimerHighValue: DWORD
); stdcall;

TimerAPCProc 的後兩個參數其實是在傳遞一個值, 使用時要把它們合并為一個 TFileTime 類型的時間.

這個時間是 APC 函數被調用的時間, 稍稍修改上面一個例子看一下:

代碼檔案:

unit Unit1;
interface
uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, StdCtrls;
type
 TForm1 = class(TForm)
  Button1: TButton;
  Button2: TButton;
  procedure Button1Click(Sender: TObject);
  procedure Button2Click(Sender: TObject);
  procedure FormDestroy(Sender: TObject);
 end;
var
 Form1: TForm1;
implementation
{$R *.dfm}
var
 hTimer: THandle;
{APC 函數}
procedure TimerAPCProc(lpArgToCompletionRoutine: Pointer; dwTimerLowValue: DWORD;
 dwTimerHighValue: DWORD); stdcall;
var
 UTCFileTime,LocalFileTime: TFileTime;
 SystemTime: TSystemTime;
 DateTime: TDateTime;
begin
 {把 dwTimerLowValue 與 dwTimerHighValue 和並為一個 TFileTime 格式的時間}
 UTCFileTime.dwLowDateTime := dwTimerLowValue;
 UTCFileTime.dwHighDateTime := dwTimerHighValue;
 FileTimeToLocalFileTime(UTCFileTime, LocalFileTime); {從世界標準計時到本地時間}
 FileTimeToSystemTime(LocalFileTime, SystemTime);   {轉到系統格式時間}
 DateTime := SystemTimeToDateTime(SystemTime);    {再轉到 TDateTime}
 Form1.Text := DateTimeToStr(DateTime);
 SleepEx(INFINITE, True);
end;
{線程入口函數}
function MyThreadFun(p: Pointer): Integer; stdcall;
var
 DueTime: Int64;
begin
 DueTime := 0;
 if SetWaitableTimer(hTimer, DueTime, 1000, @TimerAPCProc, nil, False) then
 begin
  SleepEx(INFINITE, True);
 end;
 Result := 0;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
 ID: DWORD;
begin
 if hTimer = 0 then hTimer := CreateWaitableTimer(nil, True, nil);
 CreateThread(nil, 0, @MyThreadFun, nil, 0, ID);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
 CancelWaitableTimer(hTimer);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
 CloseHandle(hTimer);
end;
end.

表單檔案:

object Form1: TForm1
 Left = 0
 Top = 0
 Caption = 'Form1'
 ClientHeight = 86
 ClientWidth = 256
 Color = clBtnFace
 Font.Charset = DEFAULT_CHARSET
 Font.Color = clWindowText
 Font.Height = -11
 Font.Name = 'Tahoma'
 Font.Style = []
 OldCreateOrder = False
 PixelsPerInch = 96
 TextHeight = 13
 object Button1: TButton
  Left = 23
  Top = 32
  Width = 97
  Height = 25
  Caption = #21551#21160#23450#26102#22120
  TabOrder = 0
  OnClick = Button1Click
 end
 object Button2: TButton
  Left = 134
  Top = 32
  Width = 97
  Height = 25
  Caption = #21462#28040#23450#26102#22120
  TabOrder = 1
  OnClick = Button2Click
 end
end

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.