Win32 API 日本語リファレンス
ホームUI.WindowsAndMessaging › SetCoalescableTimer

SetCoalescableTimer

関数
許容遅延を指定し合体可能なタイマーを作成する。
DLLUSER32.dll呼出規約winapiSetLastErrorあり対応OSwindows8.0

シグネチャ

// USER32.dll
#include <windows.h>

UINT_PTR SetCoalescableTimer(
    HWND hWnd,   // optional
    UINT_PTR nIDEvent,
    DWORD uElapse,
    TIMERPROC lpTimerFunc,   // optional
    DWORD uToleranceDelay
);

パラメーター

名前方向説明
hWndHWNDinoptionalタイマーに関連付けるウィンドウへのハンドルです。このウィンドウは呼び出し元のスレッドが所有している必要があります。hWndNULL を渡し、nIDEvent が既存のタイマーと一致する場合、そのタイマーは、既存の非 NULL の hWnd タイマーが置き換えられるのと同じ方法で置き換えられます。
nIDEventUINT_PTRinタイマー識別子です。hWnd パラメーターが NULL で、nIDEvent が既存のタイマーと一致しない場合、nIDEvent は無視され、新しいタイマー ID が生成されます。hWnd パラメーターが NULL ではなく、hWnd で指定したウィンドウが既に値 nIDEvent を持つタイマーを保有している場合、既存のタイマーは新しいタイマーに置き換えられます。SetCoalescableTimer がタイマーを置き換えると、タイマーはリセットされます。したがって、メッセージは現在のタイムアウト値が経過した後に送信され、以前に設定されたタイムアウト値は無視されます。既存のタイマーの置き換えを意図しない呼び出しでは、hWndNULL の場合、nIDEvent は 0 にする必要があります。
uElapseDWORDin

タイムアウト値(ミリ秒単位)です。

uElapseUSER_TIMER_MINIMUM (0x0000000A) 未満の場合、タイムアウトは USER_TIMER_MINIMUM に設定されます。uElapseUSER_TIMER_MAXIMUM (0x7FFFFFFF) より大きい場合、タイムアウトは USER_TIMER_MAXIMUM に設定されます。

uElapseuToleranceDelay の合計が USER_TIMER_MAXIMUM を超える場合、ERROR_INVALID_PARAMETER 例外が発生します。

lpTimerFuncTIMERPROCinoptionalタイムアウト値が経過したときに通知される関数へのポインターです。この関数の詳細については、TimerProc を参照してください。lpTimerFuncNULL の場合、システムはアプリケーションキューに WM_TIMER メッセージをポストします。メッセージの MSG 構造体の hwnd メンバーには、hWnd パラメーターの値が格納されます。
uToleranceDelayDWORDin

次のいずれかの値を指定できます。

意味
TIMERV_DEFAULT_COALESCING
0x00000000
システム既定のタイマー合体を使用します。
TIMERV_NO_COALESCING
0xFFFFFFFF
タイマー合体を使用しません。この値を使用すると、システム既定のタイマー合体やアプリケーション互換性フラグの設定にかかわらず、作成されるタイマーは合体されません。
注意 タイマーに合体が不要であると確信できる場合を除き、この値を使用しないでください。
0x1 - 0x7FFFFFF5
合体許容遅延(ミリ秒単位)を指定します。

アプリケーションでは、この値をシステム既定(TIMERV_DEFAULT_COALESCING)または可能な限り大きい値に設定してください。

uElapseuToleranceDelay の合計が USER_TIMER_MAXIMUM (0x7FFFFFFF) を超える場合、ERROR_INVALID_PARAMETER 例外が発生します。

詳細とベストプラクティスについては、Windows Timer Coalescing を参照してください。

その他の値
無効な値です。uToleranceDelay に無効な値が設定された場合、関数は失敗し、ゼロを返します。

戻り値の型: UINT_PTR

公式ドキュメント

指定したタイムアウト値と合体(コアレッシング)許容遅延を持つタイマーを作成します。

戻り値

型: UINT_PTR

関数が成功し、hWnd パラメーターが NULL の場合、戻り値は新しいタイマーを識別する整数です。アプリケーションは、この値を KillTimer 関数に渡してタイマーを破棄できます。

関数が成功し、hWnd パラメーターが NULL ではない場合、戻り値はゼロ以外の整数です。アプリケーションは、nIDEvent パラメーターの値を KillTimer 関数に渡してタイマーを破棄できます。

関数がタイマーの作成に失敗した場合、戻り値はゼロです。拡張エラー情報を取得するには、GetLastError を呼び出します。

解説(Remarks)

アプリケーションは、ウィンドウプロシージャに WM_TIMER の case 文を含めるか、タイマー作成時に TimerProc コールバック関数を指定することで、WM_TIMER メッセージを処理できます。TimerProc コールバック関数を指定した場合、既定のウィンドウプロシージャは WM_TIMER を処理する際にそのコールバック関数を呼び出します。したがって、WM_TIMER を処理する代わりに TimerProc を使用する場合でも、呼び出し元のスレッドでメッセージをディスパッチする必要があります。

WM_TIMER メッセージの wParam パラメーターには、nIDEvent パラメーターの値が格納されます。

タイマー識別子 nIDEvent は、関連付けられたウィンドウに固有です。別のウィンドウは、他のウィンドウが所有するタイマーと同じ識別子を持つ独自のタイマーを保有できます。これらのタイマーは別個のものです。

SetTimer は、hWndNULL の場合にタイマー ID を再利用できます。

uToleranceDelay が 0 に設定されている場合、システム既定のタイマー合体が使用され、SetCoalescableTimerSetTimer と同じように動作します。

SetCoalescableTimer やその他のタイマー関連関数を使用する前に、SetUserObjectInformationW 関数を通じて UOI_TIMERPROC_EXCEPTION_SUPPRESSION フラグを false に設定することが推奨されます。そうしないと、アプリケーションが予測不能な動作をし、セキュリティ上の脆弱性にさらされる可能性があります。詳細については、SetUserObjectInformationW を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// USER32.dll
#include <windows.h>

UINT_PTR SetCoalescableTimer(
    HWND hWnd,   // optional
    UINT_PTR nIDEvent,
    DWORD uElapse,
    TIMERPROC lpTimerFunc,   // optional
    DWORD uToleranceDelay
);
[DllImport("USER32.dll", SetLastError = true, ExactSpelling = true)]
static extern UIntPtr SetCoalescableTimer(
    IntPtr hWnd,   // HWND optional
    UIntPtr nIDEvent,   // UINT_PTR
    uint uElapse,   // DWORD
    IntPtr lpTimerFunc,   // TIMERPROC optional
    uint uToleranceDelay   // DWORD
);
<DllImport("USER32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetCoalescableTimer(
    hWnd As IntPtr,   ' HWND optional
    nIDEvent As UIntPtr,   ' UINT_PTR
    uElapse As UInteger,   ' DWORD
    lpTimerFunc As IntPtr,   ' TIMERPROC optional
    uToleranceDelay As UInteger   ' DWORD
) As UIntPtr
End Function
' hWnd : HWND optional
' nIDEvent : UINT_PTR
' uElapse : DWORD
' lpTimerFunc : TIMERPROC optional
' uToleranceDelay : DWORD
Declare PtrSafe Function SetCoalescableTimer Lib "user32" ( _
    ByVal hWnd As LongPtr, _
    ByVal nIDEvent As LongPtr, _
    ByVal uElapse As Long, _
    ByVal lpTimerFunc As LongPtr, _
    ByVal uToleranceDelay As Long) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetCoalescableTimer = ctypes.windll.user32.SetCoalescableTimer
SetCoalescableTimer.restype = ctypes.c_size_t
SetCoalescableTimer.argtypes = [
    wintypes.HANDLE,  # hWnd : HWND optional
    ctypes.c_size_t,  # nIDEvent : UINT_PTR
    wintypes.DWORD,  # uElapse : DWORD
    ctypes.c_void_p,  # lpTimerFunc : TIMERPROC optional
    wintypes.DWORD,  # uToleranceDelay : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USER32.dll')
SetCoalescableTimer = Fiddle::Function.new(
  lib['SetCoalescableTimer'],
  [
    Fiddle::TYPE_VOIDP,  # hWnd : HWND optional
    Fiddle::TYPE_UINTPTR_T,  # nIDEvent : UINT_PTR
    -Fiddle::TYPE_INT,  # uElapse : DWORD
    Fiddle::TYPE_VOIDP,  # lpTimerFunc : TIMERPROC optional
    -Fiddle::TYPE_INT,  # uToleranceDelay : DWORD
  ],
  Fiddle::TYPE_UINTPTR_T)
#[link(name = "user32")]
extern "system" {
    fn SetCoalescableTimer(
        hWnd: *mut core::ffi::c_void,  // HWND optional
        nIDEvent: usize,  // UINT_PTR
        uElapse: u32,  // DWORD
        lpTimerFunc: *const core::ffi::c_void,  // TIMERPROC optional
        uToleranceDelay: u32  // DWORD
    ) -> usize;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("USER32.dll", SetLastError = true)]
public static extern UIntPtr SetCoalescableTimer(IntPtr hWnd, UIntPtr nIDEvent, uint uElapse, IntPtr lpTimerFunc, uint uToleranceDelay);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USER32_SetCoalescableTimer' -Namespace Win32 -PassThru
# $api::SetCoalescableTimer(hWnd, nIDEvent, uElapse, lpTimerFunc, uToleranceDelay)
#uselib "USER32.dll"
#func global SetCoalescableTimer "SetCoalescableTimer" sptr, sptr, sptr, sptr, sptr
; SetCoalescableTimer hWnd, nIDEvent, uElapse, lpTimerFunc, uToleranceDelay   ; 戻り値は stat
; hWnd : HWND optional -> "sptr"
; nIDEvent : UINT_PTR -> "sptr"
; uElapse : DWORD -> "sptr"
; lpTimerFunc : TIMERPROC optional -> "sptr"
; uToleranceDelay : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "USER32.dll"
#cfunc global SetCoalescableTimer "SetCoalescableTimer" sptr, sptr, int, sptr, int
; res = SetCoalescableTimer(hWnd, nIDEvent, uElapse, lpTimerFunc, uToleranceDelay)
; hWnd : HWND optional -> "sptr"
; nIDEvent : UINT_PTR -> "sptr"
; uElapse : DWORD -> "int"
; lpTimerFunc : TIMERPROC optional -> "sptr"
; uToleranceDelay : DWORD -> "int"
; UINT_PTR SetCoalescableTimer(HWND hWnd, UINT_PTR nIDEvent, DWORD uElapse, TIMERPROC lpTimerFunc, DWORD uToleranceDelay)
#uselib "USER32.dll"
#cfunc global SetCoalescableTimer "SetCoalescableTimer" intptr, intptr, int, intptr, int
; res = SetCoalescableTimer(hWnd, nIDEvent, uElapse, lpTimerFunc, uToleranceDelay)
; hWnd : HWND optional -> "intptr"
; nIDEvent : UINT_PTR -> "intptr"
; uElapse : DWORD -> "int"
; lpTimerFunc : TIMERPROC optional -> "intptr"
; uToleranceDelay : DWORD -> "int"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	user32 = windows.NewLazySystemDLL("USER32.dll")
	procSetCoalescableTimer = user32.NewProc("SetCoalescableTimer")
)

// hWnd (HWND optional), nIDEvent (UINT_PTR), uElapse (DWORD), lpTimerFunc (TIMERPROC optional), uToleranceDelay (DWORD)
r1, _, err := procSetCoalescableTimer.Call(
	uintptr(hWnd),
	uintptr(nIDEvent),
	uintptr(uElapse),
	uintptr(lpTimerFunc),
	uintptr(uToleranceDelay),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // UINT_PTR
function SetCoalescableTimer(
  hWnd: THandle;   // HWND optional
  nIDEvent: NativeUInt;   // UINT_PTR
  uElapse: DWORD;   // DWORD
  lpTimerFunc: Pointer;   // TIMERPROC optional
  uToleranceDelay: DWORD   // DWORD
): NativeUInt; stdcall;
  external 'USER32.dll' name 'SetCoalescableTimer';
result := DllCall("USER32\SetCoalescableTimer"
    , "Ptr", hWnd   ; HWND optional
    , "UPtr", nIDEvent   ; UINT_PTR
    , "UInt", uElapse   ; DWORD
    , "Ptr", lpTimerFunc   ; TIMERPROC optional
    , "UInt", uToleranceDelay   ; DWORD
    , "UPtr")   ; return: UINT_PTR
●SetCoalescableTimer(hWnd, nIDEvent, uElapse, lpTimerFunc, uToleranceDelay) = DLL("USER32.dll", "int SetCoalescableTimer(void*, int, dword, void*, dword)")
# 呼び出し: SetCoalescableTimer(hWnd, nIDEvent, uElapse, lpTimerFunc, uToleranceDelay)
# hWnd : HWND optional -> "void*"
# nIDEvent : UINT_PTR -> "int"
# uElapse : DWORD -> "dword"
# lpTimerFunc : TIMERPROC optional -> "void*"
# uToleranceDelay : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "user32" fn SetCoalescableTimer(
    hWnd: ?*anyopaque, // HWND optional
    nIDEvent: usize, // UINT_PTR
    uElapse: u32, // DWORD
    lpTimerFunc: ?*anyopaque, // TIMERPROC optional
    uToleranceDelay: u32 // DWORD
) callconv(std.os.windows.WINAPI) usize;
proc SetCoalescableTimer(
    hWnd: pointer,  # HWND optional
    nIDEvent: uint,  # UINT_PTR
    uElapse: uint32,  # DWORD
    lpTimerFunc: pointer,  # TIMERPROC optional
    uToleranceDelay: uint32  # DWORD
): uint {.importc: "SetCoalescableTimer", stdcall, dynlib: "USER32.dll".}
pragma(lib, "user32");
extern(Windows)
size_t SetCoalescableTimer(
    void* hWnd,   // HWND optional
    size_t nIDEvent,   // UINT_PTR
    uint uElapse,   // DWORD
    void* lpTimerFunc,   // TIMERPROC optional
    uint uToleranceDelay   // DWORD
);
ccall((:SetCoalescableTimer, "USER32.dll"), stdcall, Csize_t,
      (Ptr{Cvoid}, Csize_t, UInt32, Ptr{Cvoid}, UInt32),
      hWnd, nIDEvent, uElapse, lpTimerFunc, uToleranceDelay)
# hWnd : HWND optional -> Ptr{Cvoid}
# nIDEvent : UINT_PTR -> Csize_t
# uElapse : DWORD -> UInt32
# lpTimerFunc : TIMERPROC optional -> Ptr{Cvoid}
# uToleranceDelay : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uintptr_t SetCoalescableTimer(
    void* hWnd,
    uintptr_t nIDEvent,
    uint32_t uElapse,
    void* lpTimerFunc,
    uint32_t uToleranceDelay);
]]
local user32 = ffi.load("user32")
-- user32.SetCoalescableTimer(hWnd, nIDEvent, uElapse, lpTimerFunc, uToleranceDelay)
-- hWnd : HWND optional
-- nIDEvent : UINT_PTR
-- uElapse : DWORD
-- lpTimerFunc : TIMERPROC optional
-- uToleranceDelay : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('USER32.dll');
const SetCoalescableTimer = lib.func('__stdcall', 'SetCoalescableTimer', 'uintptr_t', ['void *', 'uintptr_t', 'uint32_t', 'void *', 'uint32_t']);
// SetCoalescableTimer(hWnd, nIDEvent, uElapse, lpTimerFunc, uToleranceDelay)
// hWnd : HWND optional -> 'void *'
// nIDEvent : UINT_PTR -> 'uintptr_t'
// uElapse : DWORD -> 'uint32_t'
// lpTimerFunc : TIMERPROC optional -> 'void *'
// uToleranceDelay : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
// コールバック(関数ポインタ)は koffi.proto/koffi.register で型を定義して渡す(素の void* では JS 関数を渡せない)。
const lib = Deno.dlopen("USER32.dll", {
  SetCoalescableTimer: { parameters: ["pointer", "usize", "u32", "pointer", "u32"], result: "usize" },
});
// lib.symbols.SetCoalescableTimer(hWnd, nIDEvent, uElapse, lpTimerFunc, uToleranceDelay)
// hWnd : HWND optional -> "pointer"
// nIDEvent : UINT_PTR -> "usize"
// uElapse : DWORD -> "u32"
// lpTimerFunc : TIMERPROC optional -> "pointer"
// uToleranceDelay : DWORD -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
size_t SetCoalescableTimer(
    void* hWnd,
    size_t nIDEvent,
    uint32_t uElapse,
    void* lpTimerFunc,
    uint32_t uToleranceDelay);
C, "USER32.dll");
// $ffi->SetCoalescableTimer(hWnd, nIDEvent, uElapse, lpTimerFunc, uToleranceDelay);
// hWnd : HWND optional
// nIDEvent : UINT_PTR
// uElapse : DWORD
// lpTimerFunc : TIMERPROC optional
// uToleranceDelay : DWORD
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface User32 extends StdCallLibrary {
    User32 INSTANCE = Native.load("user32", User32.class);
    long SetCoalescableTimer(
        Pointer hWnd,   // HWND optional
        long nIDEvent,   // UINT_PTR
        int uElapse,   // DWORD
        Callback lpTimerFunc,   // TIMERPROC optional
        int uToleranceDelay   // DWORD
    );
}
@[Link("user32")]
lib LibUSER32
  fun SetCoalescableTimer = SetCoalescableTimer(
    hWnd : Void*,   # HWND optional
    nIDEvent : LibC::SizeT,   # UINT_PTR
    uElapse : UInt32,   # DWORD
    lpTimerFunc : Void*,   # TIMERPROC optional
    uToleranceDelay : UInt32   # DWORD
  ) : LibC::SizeT
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef SetCoalescableTimerNative = UintPtr Function(Pointer<Void>, UintPtr, Uint32, Pointer<Void>, Uint32);
typedef SetCoalescableTimerDart = int Function(Pointer<Void>, int, int, Pointer<Void>, int);
final SetCoalescableTimer = DynamicLibrary.open('USER32.dll')
    .lookupFunction<SetCoalescableTimerNative, SetCoalescableTimerDart>('SetCoalescableTimer');
// hWnd : HWND optional -> Pointer<Void>
// nIDEvent : UINT_PTR -> UintPtr
// uElapse : DWORD -> Uint32
// lpTimerFunc : TIMERPROC optional -> Pointer<Void>
// uToleranceDelay : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SetCoalescableTimer(
  hWnd: THandle;   // HWND optional
  nIDEvent: NativeUInt;   // UINT_PTR
  uElapse: DWORD;   // DWORD
  lpTimerFunc: Pointer;   // TIMERPROC optional
  uToleranceDelay: DWORD   // DWORD
): NativeUInt; stdcall;
  external 'USER32.dll' name 'SetCoalescableTimer';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SetCoalescableTimer"
  c_SetCoalescableTimer :: Ptr () -> CUIntPtr -> Word32 -> Ptr () -> Word32 -> IO CUIntPtr
-- hWnd : HWND optional -> Ptr ()
-- nIDEvent : UINT_PTR -> CUIntPtr
-- uElapse : DWORD -> Word32
-- lpTimerFunc : TIMERPROC optional -> Ptr ()
-- uToleranceDelay : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let setcoalescabletimer =
  foreign "SetCoalescableTimer"
    ((ptr void) @-> size_t @-> uint32_t @-> (ptr void) @-> uint32_t @-> returning size_t)
(* hWnd : HWND optional -> (ptr void) *)
(* nIDEvent : UINT_PTR -> size_t *)
(* uElapse : DWORD -> uint32_t *)
(* lpTimerFunc : TIMERPROC optional -> (ptr void) *)
(* uToleranceDelay : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library user32 (t "USER32.dll"))
(cffi:use-foreign-library user32)

(cffi:defcfun ("SetCoalescableTimer" set-coalescable-timer :convention :stdcall) :uint64
  (h-wnd :pointer)   ; HWND optional
  (n-idevent :uint64)   ; UINT_PTR
  (u-elapse :uint32)   ; DWORD
  (lp-timer-func :pointer)   ; TIMERPROC optional
  (u-tolerance-delay :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SetCoalescableTimer = Win32::API::More->new('USER32',
    'WPARAM SetCoalescableTimer(HANDLE hWnd, WPARAM nIDEvent, DWORD uElapse, LPVOID lpTimerFunc, DWORD uToleranceDelay)');
# my $ret = $SetCoalescableTimer->Call($hWnd, $nIDEvent, $uElapse, $lpTimerFunc, $uToleranceDelay);
# hWnd : HWND optional -> HANDLE
# nIDEvent : UINT_PTR -> WPARAM
# uElapse : DWORD -> DWORD
# lpTimerFunc : TIMERPROC optional -> LPVOID
# uToleranceDelay : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# コールバック(関数ポインタ)は Perl sub を直接渡せません。Win32::API::Callback を使用してください。

関連項目

公式の関連項目
使用する型