Multithreaded Programming (14)

Source: Internet
Author: User
Tags bool relative semaphore thread time interval

function CreateWaitableTimer(
 lpTimerAttributes: PSecurityAttributes; {安全}
 bManualReset: BOOL;  {True: 可调度多个线程; False: 只调度一个线程}
 lpTimerName: PWideChar {名称}
): THandle; stdcall;   {返回句柄}
function SetWaitableTimer(
 hTimer: THandle;             {句柄}
 var lpDueTime: TLargeInteger;      {起始时间}
 lPeriod: Longint;            {间隔时间}
 pfnCompletionRoutine: TFNTimerAPCRoutine;{回调函数的指针}
 lpArgToCompletionRoutine: Pointer;    {给回调函数的参数}
 fResume: BOOL              {是否唤醒系统}
): BOOL; stdcall;             {}

The Waitabletimer object is more complex, and its basic idea is to let the waiting thread run at a specified time.

Like other similar objects, first establish (CreateWaitableTimer), establish a function of the second parameter to decide whether to schedule a thread or all waiting threads; This is somewhat similar to the Signal object (semaphore), although semaphore can specify the specific number of drivers that can be driven.

Unlike other similar objects: After CreateWaitableTimer, the Waitabletimer object did not immediately begin to work;

Then call the SetWaitableTimer function to make it work. It's kind of like an Event object.

The SetWaitableTimer function is more cumbersome, and it has to be done slowly, for example:

var
hwaitabletimer:thandle; {The handle variable for the Waitabletimer object should be global}
Procedure Tform1.button1click (sender:tobject);
var
duetime:int64
Begin
{Establish Waitabletimer object and return handle}
Hwaitabletimer: = CreateWaitableTimer (Nil, Tr UE, NIL); {The middle of True indicates that multiple threads can be driven}
Duetime: = 0; {This will be the second parameter of the SetWaitableTimer, because it is the Var argument and cannot be given directly to the constant}       
SetWaitableTimer (Hwaitabletimer, {Waitabletimer object handle}
Duetime, {Starting time, here is 0}
0, {time interval, here is also 0}
Nil, {No callback function}
Nil, {Of course, do not need to give a callback function parameter}
False {This value is True, even if the system is in the screensaver or standby state, Time to thread and system will all be awakened!}
);
End;
{again:
the start time (the second argument) has three assignment methods:
1, > 0 o'clock is an absolute time, is a tfiletime format of time (after detailed assignment method);
2, < 0 o'clock is relative time, relative to the current, such as -50000000 indicates 5 seconds after execution (in 0.1 milliseconds, in detail);
3, = 0 o'clock, execute immediately, no longer wait; the example above and the first example below we first use 0. The
interval (the third parameter) has two scenarios:
1, for example, 5000 is performed every 5 seconds, in milliseconds; the second example of this page uses 500 (half a second);
2, if assigned to 0, indicates that it is executed only once, based on the start time, and is no longer repeated.
The callback function and its arguments (argument fourth to fifth), whichWill involve a more complex topic (APC), not using it for the time being, and later.
The last parameter has been made clear, I also tested (respectively in the screensaver and standby), very effective!
}

The first example we will try to use it as simply as possible (but this does not show its advantages):

When CreateWaitableTimer, we decide to let it control multiple threads;

SetWaitableTimer it immediately participates in control, executes only once, and does not use callback functions.

This example effect chart:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.