CreateWaitableTimer

封裝了一個類,爲了對大限度的降低耦合,簡化使用時的編碼,決定使用下可等待計時器,以替換掉OnTimer。各種資料上面說的不夠完整,大多也是抄襲的,所以打算自己看下原文API,順便翻譯下。

====================出雙入對的分割線======================

原文來自MSDN,原文鏈接:http://msdn.microsoft.com/en-us/library/windows/desktop/ms682492%28v=vs.85%29.aspx

本文僅作翻譯、註釋。

本文鏈接:http://blog.csdn.net/wlsgzl/article/details/38872123

轉載請保持文章的完整性,並在顯要的註明作者和出處。

-------------------------------------秀恩愛 分的快--------------------------------------

CreateWaitableTimer Function

CreateWaitableTimer function

Creates or opens a waitable timer object.

To specify an access mask for the object, use the CreateWaitableTimerEx function.

Syntax

HANDLE WINAPI CreateWaitableTimer(//防盜,本文作者http://blog.csdn.net/wlsgzl
  _In_opt_  LPSECURITY_ATTRIBUTES lpTimerAttributes,
  _In_      BOOL bManualReset,
  _In_opt_  LPCTSTR lpTimerName
);

Parameters

lpTimerAttributes [in, optional]

A pointer to a SECURITY_ATTRIBUTES structure that specifies a security descriptor for the new timer object and determines whether child processes can inherit the returned handle.

If lpTimerAttributes is NULL, the timer object gets a default security descriptor and the handle cannot be inherited. The ACLs in the default security descriptor for a timer come from the primary or impersonation token of the creator.

bManualReset [in]

If this parameter is TRUE, the timer is a manual-reset notification timer. Otherwise, the timer is a synchronization timer.

lpTimerName [in, optional]

The name of the timer object. The name is limited to MAX_PATH characters. Name comparison is case sensitive.

If lpTimerName is NULL, the timer object is created without a name.

If lpTimerName matches the name of an existing event, semaphore, mutex, job, or file-mapping object, the function fails andGetLastError returns ERROR_INVALID_HANDLE. This occurs because these objects share the same namespace.

The name can have a "Global\" or "Local\" prefix to explicitly create the object in the global or session namespace. The remainder of the name can contain any character except the backslash character (\). For more information, seeKernel Object Namespaces. Fast user switching is implemented using Terminal Services sessions. Kernel object names must follow the guidelines outlined for Terminal Services so that applications can support multiple users.

The object can be created in a private namespace. For more information, see Object Namespaces.

Return value

If the function succeeds, the return value is a handle to the timer object. If the named timer object exists before the function call, the function returns a handle to the existing object andGetLastError returns ERROR_ALREADY_EXISTS.

If the function fails, the return value is NULL. To get extended error information, callGetLastError.

Remarks

The handle returned by CreateWaitableTimer is created with theTIMER_ALL_ACCESS access right; it can be used in any function that requires a handle to a timer object, provided that the caller has been granted access. If a timer is created from a service or thread that is impersonating a different user, you can either apply a security descriptor to the timer when you create it, or change the default security descriptor for the creating process by changing its default DACL. For more information, seeSynchronization Object Security and Access Rights.

Any thread of the calling process can specify the timer object handle in a call to one of thewait functions.

Multiple processes can have handles to the same timer object, enabling use of the object for interprocess synchronization.

  • A process created by the CreateProcess function can inherit a handle to a timer object if thelpTimerAttributes parameter ofCreateWaitableTimer enables inheritance.
  • A process can specify the timer object handle in a call to the DuplicateHandle function. The resulting handle can be used by another process.
  • A process can specify the name of a timer object in a call to the OpenWaitableTimer or CreateWaitableTimer function.

Use the CloseHandle function to close the handle. The system closes the handle automatically when the process terminates. The timer object is destroyed when its last handle has been closed.

To compile an application that uses this function, define _WIN32_WINNT as 0x0400 or later. For more information, seeUsing the Windows Headers.

To associate a timer with a window, use the SetTimer function.

Examples

For an example that uses CreateWaitableTimer, see Using Waitable Timer Objects.(本文鏈接http://blog.csdn.net/wlsgzl/article/details/38872123

Requirements

Minimum supported client

Windows XP [desktop apps only]

Minimum supported server

Windows Server 2003 [desktop apps only]

Header

WinBase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

Unicode and ANSI names

CreateWaitableTimerW (Unicode) and CreateWaitableTimerA (ANSI)

See also

CancelWaitableTimer
CloseHandle
CreateProcess
CreateWaitableTimerEx
DuplicateHandle
FILETIME
Object Names
OpenWaitableTimer
SECURITY_ATTRIBUTES
SetWaitableTimer
Synchronization Functions
Waitable Timer Objects

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章