Win32 API 日本語リファレンス
ホームSystem.Threading › SetThreadpoolTimerEx

SetThreadpoolTimerEx

関数
スレッドプールタイマーの発火時刻と周期を設定する(拡張版)。
DLLKERNEL32.dll呼出規約winapi対応OSwindows8.0

シグネチャ

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

BOOL SetThreadpoolTimerEx(
    PTP_TIMER pti,
    FILETIME* pftDueTime,   // optional
    DWORD msPeriod,
    DWORD msWindowLength   // optional
);

パラメーター

名前方向説明
ptiPTP_TIMERin設定するタイマーオブジェクトを定義する TP_TIMER 構造体へのポインターです。このポインターは CreateThreadpoolTimer 関数によって返されます。
pftDueTimeFILETIME*inoptional

タイマーが期限切れになるべき絶対時刻または相対時刻を指定する FILETIME 構造体へのポインターです。このパラメーターが正の値を指している場合は、1601 年 1 月 1 日(UTC)からの絶対時刻を 100 ナノ秒単位で示します。負の値を指している場合は、現在時刻からの相対的な待機時間を示します。ゼロを指している場合は、タイマーは直ちに期限切れになります。時刻値の詳細については、File Times を参照してください。

このパラメーターが NULL の場合、タイマーオブジェクトは新しいコールバックのキューへの登録を停止します(ただし、すでにキューに登録済みのコールバックは引き続き発生します)。

pftDueTime パラメーターが NULL 以外の場合、タイマーが設定されます。

msPeriodDWORDinタイマーの周期(ミリ秒単位)です。このパラメーターがゼロの場合、タイマーは一度だけシグナル状態になります。ゼロより大きい場合、タイマーは周期的になります。周期的なタイマーは、キャンセルされるまで、周期が経過するたびに自動的に再起動します。
msWindowLengthDWORDinoptionalシステムがタイマーコールバックを呼び出すまでに遅延できる最大時間です。このパラメーターが設定されている場合、システムは電力を節約するために呼び出しをまとめて処理できます。

戻り値の型: BOOL

公式ドキュメント

タイマーオブジェクトを設定し、既存のタイマーがあればそれを置き換えます。指定したタイムアウトが経過すると、ワーカースレッドがタイマーオブジェクトのコールバックを呼び出します。(SetThreadpoolTimerEx)

戻り値

タイマーが以前に設定されており、キャンセルされた場合は TRUE を返します。 それ以外の場合は FALSE を返します。

タイマーの以前の状態が「設定済み」で、関数が FALSE を返した場合は、コールバックが進行中であるか、まもなく開始されようとしています。 詳細については「解説」を参照してください。

解説(Remarks)

タイマーを設定すると、既存のタイマーがあればキャンセルされます。

場合によっては、アプリケーションがスレッドプールタイマーを閉じた後にコールバック関数が実行されることがあります。この動作を防ぐには、アプリケーションは CloseThreadpoolTimer に記載された手順に従う必要があります。

pftDueTime で指定された期限が相対時刻の場合、システムがスリープまたは休止状態に費やす時間は、タイマーの期限切れには算入されません。タイマーは、システムが起動状態で費やした経過時間の累計がタイマーの相対的な期限または周期に達したときにシグナル状態になります。pftDueTime で指定された期限が絶対時刻の場合、システムがスリープまたは休止状態に費やす時間は、タイマーの期限切れに算入されます。システムがスリープ中にタイマーが期限切れになった場合、システムが起動したときにタイマーは直ちにシグナル状態になります。

この関数を使用するアプリケーションをコンパイルするには、_WIN32_WINNT を 0x0600 以上として定義してください。

Examples

例については、Using the Thread Pool Functions を参照してください。

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

各言語での呼び出し定義

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

BOOL SetThreadpoolTimerEx(
    PTP_TIMER pti,
    FILETIME* pftDueTime,   // optional
    DWORD msPeriod,
    DWORD msWindowLength   // optional
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", ExactSpelling = true)]
static extern bool SetThreadpoolTimerEx(
    IntPtr pti,   // PTP_TIMER
    IntPtr pftDueTime,   // FILETIME* optional
    uint msPeriod,   // DWORD
    uint msWindowLength   // DWORD optional
);
<DllImport("KERNEL32.dll", ExactSpelling:=True)>
Public Shared Function SetThreadpoolTimerEx(
    pti As IntPtr,   ' PTP_TIMER
    pftDueTime As IntPtr,   ' FILETIME* optional
    msPeriod As UInteger,   ' DWORD
    msWindowLength As UInteger   ' DWORD optional
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
' pti : PTP_TIMER
' pftDueTime : FILETIME* optional
' msPeriod : DWORD
' msWindowLength : DWORD optional
Declare PtrSafe Function SetThreadpoolTimerEx Lib "kernel32" ( _
    ByVal pti As LongPtr, _
    ByVal pftDueTime As LongPtr, _
    ByVal msPeriod As Long, _
    ByVal msWindowLength As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetThreadpoolTimerEx = ctypes.windll.kernel32.SetThreadpoolTimerEx
SetThreadpoolTimerEx.restype = wintypes.BOOL
SetThreadpoolTimerEx.argtypes = [
    ctypes.c_ssize_t,  # pti : PTP_TIMER
    ctypes.c_void_p,  # pftDueTime : FILETIME* optional
    wintypes.DWORD,  # msPeriod : DWORD
    wintypes.DWORD,  # msWindowLength : DWORD optional
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
SetThreadpoolTimerEx = Fiddle::Function.new(
  lib['SetThreadpoolTimerEx'],
  [
    Fiddle::TYPE_INTPTR_T,  # pti : PTP_TIMER
    Fiddle::TYPE_VOIDP,  # pftDueTime : FILETIME* optional
    -Fiddle::TYPE_INT,  # msPeriod : DWORD
    -Fiddle::TYPE_INT,  # msWindowLength : DWORD optional
  ],
  Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn SetThreadpoolTimerEx(
        pti: isize,  // PTP_TIMER
        pftDueTime: *mut FILETIME,  // FILETIME* optional
        msPeriod: u32,  // DWORD
        msWindowLength: u32  // DWORD optional
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll")]
public static extern bool SetThreadpoolTimerEx(IntPtr pti, IntPtr pftDueTime, uint msPeriod, uint msWindowLength);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_SetThreadpoolTimerEx' -Namespace Win32 -PassThru
# $api::SetThreadpoolTimerEx(pti, pftDueTime, msPeriod, msWindowLength)
#uselib "KERNEL32.dll"
#func global SetThreadpoolTimerEx "SetThreadpoolTimerEx" sptr, sptr, sptr, sptr
; SetThreadpoolTimerEx pti, varptr(pftDueTime), msPeriod, msWindowLength   ; 戻り値は stat
; pti : PTP_TIMER -> "sptr"
; pftDueTime : FILETIME* optional -> "sptr"
; msPeriod : DWORD -> "sptr"
; msWindowLength : DWORD optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "KERNEL32.dll"
#cfunc global SetThreadpoolTimerEx "SetThreadpoolTimerEx" sptr, var, int, int
; res = SetThreadpoolTimerEx(pti, pftDueTime, msPeriod, msWindowLength)
; pti : PTP_TIMER -> "sptr"
; pftDueTime : FILETIME* optional -> "var"
; msPeriod : DWORD -> "int"
; msWindowLength : DWORD optional -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL SetThreadpoolTimerEx(PTP_TIMER pti, FILETIME* pftDueTime, DWORD msPeriod, DWORD msWindowLength)
#uselib "KERNEL32.dll"
#cfunc global SetThreadpoolTimerEx "SetThreadpoolTimerEx" intptr, var, int, int
; res = SetThreadpoolTimerEx(pti, pftDueTime, msPeriod, msWindowLength)
; pti : PTP_TIMER -> "intptr"
; pftDueTime : FILETIME* optional -> "var"
; msPeriod : DWORD -> "int"
; msWindowLength : DWORD optional -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procSetThreadpoolTimerEx = kernel32.NewProc("SetThreadpoolTimerEx")
)

// pti (PTP_TIMER), pftDueTime (FILETIME* optional), msPeriod (DWORD), msWindowLength (DWORD optional)
r1, _, err := procSetThreadpoolTimerEx.Call(
	uintptr(pti),
	uintptr(pftDueTime),
	uintptr(msPeriod),
	uintptr(msWindowLength),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function SetThreadpoolTimerEx(
  pti: NativeInt;   // PTP_TIMER
  pftDueTime: Pointer;   // FILETIME* optional
  msPeriod: DWORD;   // DWORD
  msWindowLength: DWORD   // DWORD optional
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'SetThreadpoolTimerEx';
result := DllCall("KERNEL32\SetThreadpoolTimerEx"
    , "Ptr", pti   ; PTP_TIMER
    , "Ptr", pftDueTime   ; FILETIME* optional
    , "UInt", msPeriod   ; DWORD
    , "UInt", msWindowLength   ; DWORD optional
    , "Int")   ; return: BOOL
●SetThreadpoolTimerEx(pti, pftDueTime, msPeriod, msWindowLength) = DLL("KERNEL32.dll", "bool SetThreadpoolTimerEx(int, void*, dword, dword)")
# 呼び出し: SetThreadpoolTimerEx(pti, pftDueTime, msPeriod, msWindowLength)
# pti : PTP_TIMER -> "int"
# pftDueTime : FILETIME* optional -> "void*"
# msPeriod : DWORD -> "dword"
# msWindowLength : DWORD optional -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "kernel32" fn SetThreadpoolTimerEx(
    pti: isize, // PTP_TIMER
    pftDueTime: [*c]FILETIME, // FILETIME* optional
    msPeriod: u32, // DWORD
    msWindowLength: u32 // DWORD optional
) callconv(std.os.windows.WINAPI) i32;
proc SetThreadpoolTimerEx(
    pti: int,  # PTP_TIMER
    pftDueTime: ptr FILETIME,  # FILETIME* optional
    msPeriod: uint32,  # DWORD
    msWindowLength: uint32  # DWORD optional
): int32 {.importc: "SetThreadpoolTimerEx", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
int SetThreadpoolTimerEx(
    ptrdiff_t pti,   // PTP_TIMER
    FILETIME* pftDueTime,   // FILETIME* optional
    uint msPeriod,   // DWORD
    uint msWindowLength   // DWORD optional
);
ccall((:SetThreadpoolTimerEx, "KERNEL32.dll"), stdcall, Int32,
      (Int, Ptr{FILETIME}, UInt32, UInt32),
      pti, pftDueTime, msPeriod, msWindowLength)
# pti : PTP_TIMER -> Int
# pftDueTime : FILETIME* optional -> Ptr{FILETIME}
# msPeriod : DWORD -> UInt32
# msWindowLength : DWORD optional -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
int32_t SetThreadpoolTimerEx(
    intptr_t pti,
    void* pftDueTime,
    uint32_t msPeriod,
    uint32_t msWindowLength);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.SetThreadpoolTimerEx(pti, pftDueTime, msPeriod, msWindowLength)
-- pti : PTP_TIMER
-- pftDueTime : FILETIME* optional
-- msPeriod : DWORD
-- msWindowLength : DWORD optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const SetThreadpoolTimerEx = lib.func('__stdcall', 'SetThreadpoolTimerEx', 'int32_t', ['intptr_t', 'void *', 'uint32_t', 'uint32_t']);
// SetThreadpoolTimerEx(pti, pftDueTime, msPeriod, msWindowLength)
// pti : PTP_TIMER -> 'intptr_t'
// pftDueTime : FILETIME* optional -> 'void *'
// msPeriod : DWORD -> 'uint32_t'
// msWindowLength : DWORD optional -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  SetThreadpoolTimerEx: { parameters: ["isize", "pointer", "u32", "u32"], result: "i32" },
});
// lib.symbols.SetThreadpoolTimerEx(pti, pftDueTime, msPeriod, msWindowLength)
// pti : PTP_TIMER -> "isize"
// pftDueTime : FILETIME* optional -> "pointer"
// msPeriod : DWORD -> "u32"
// msWindowLength : DWORD optional -> "u32"
// 文字列引数は "buffer"(NUL 終端のバイト列を Uint8Array で渡す)。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
int32_t SetThreadpoolTimerEx(
    intptr_t pti,
    void* pftDueTime,
    uint32_t msPeriod,
    uint32_t msWindowLength);
C, "KERNEL32.dll");
// $ffi->SetThreadpoolTimerEx(pti, pftDueTime, msPeriod, msWindowLength);
// pti : PTP_TIMER
// pftDueTime : FILETIME* optional
// msPeriod : DWORD
// msWindowLength : DWORD optional
// 構造体/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 Kernel32 extends StdCallLibrary {
    Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class);
    boolean SetThreadpoolTimerEx(
        long pti,   // PTP_TIMER
        Pointer pftDueTime,   // FILETIME* optional
        int msPeriod,   // DWORD
        int msWindowLength   // DWORD optional
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun SetThreadpoolTimerEx = SetThreadpoolTimerEx(
    pti : LibC::SSizeT,   # PTP_TIMER
    pftDueTime : FILETIME*,   # FILETIME* optional
    msPeriod : UInt32,   # DWORD
    msWindowLength : UInt32   # DWORD optional
  ) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef SetThreadpoolTimerExNative = Int32 Function(IntPtr, Pointer<Void>, Uint32, Uint32);
typedef SetThreadpoolTimerExDart = int Function(int, Pointer<Void>, int, int);
final SetThreadpoolTimerEx = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<SetThreadpoolTimerExNative, SetThreadpoolTimerExDart>('SetThreadpoolTimerEx');
// pti : PTP_TIMER -> IntPtr
// pftDueTime : FILETIME* optional -> Pointer<Void>
// msPeriod : DWORD -> Uint32
// msWindowLength : DWORD optional -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SetThreadpoolTimerEx(
  pti: NativeInt;   // PTP_TIMER
  pftDueTime: Pointer;   // FILETIME* optional
  msPeriod: DWORD;   // DWORD
  msWindowLength: DWORD   // DWORD optional
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'SetThreadpoolTimerEx';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SetThreadpoolTimerEx"
  c_SetThreadpoolTimerEx :: CIntPtr -> Ptr () -> Word32 -> Word32 -> IO CInt
-- pti : PTP_TIMER -> CIntPtr
-- pftDueTime : FILETIME* optional -> Ptr ()
-- msPeriod : DWORD -> Word32
-- msWindowLength : DWORD optional -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let setthreadpooltimerex =
  foreign "SetThreadpoolTimerEx"
    (intptr_t @-> (ptr void) @-> uint32_t @-> uint32_t @-> returning int32_t)
(* pti : PTP_TIMER -> intptr_t *)
(* pftDueTime : FILETIME* optional -> (ptr void) *)
(* msPeriod : DWORD -> uint32_t *)
(* msWindowLength : DWORD optional -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)

(cffi:defcfun ("SetThreadpoolTimerEx" set-threadpool-timer-ex :convention :stdcall) :int32
  (pti :int64)   ; PTP_TIMER
  (pft-due-time :pointer)   ; FILETIME* optional
  (ms-period :uint32)   ; DWORD
  (ms-window-length :uint32))   ; DWORD optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SetThreadpoolTimerEx = Win32::API::More->new('KERNEL32',
    'BOOL SetThreadpoolTimerEx(LPARAM pti, LPVOID pftDueTime, DWORD msPeriod, DWORD msWindowLength)');
# my $ret = $SetThreadpoolTimerEx->Call($pti, $pftDueTime, $msPeriod, $msWindowLength);
# pti : PTP_TIMER -> LPARAM
# pftDueTime : FILETIME* optional -> LPVOID
# msPeriod : DWORD -> DWORD
# msWindowLength : DWORD optional -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

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