using System; using System.Runtime.InteropServices; using FILETIME = System.Runtime.InteropServices.ComTypes.FILETIME; namespace Vanara.PInvoke { public static partial class Kernel32 { /// /// Applications implement this callback if they call the SetThreadpoolCallbackCleanupGroup function to specify the callback to use /// when CloseThreadpoolCleanupGroup is called. /// /// Optional application-defined data specified during creation of the object. /// Optional application-defined data specified using CloseThreadpoolCleanupGroupMembers. [UnmanagedFunctionPointer(CallingConvention.Winapi)] public delegate void CleanupGroupCancelCallback(IntPtr ObjectContext, IntPtr CleanupContext); /// /// Applications implement this callback if they call the StartThreadpoolIo function to start a worker thread for the I/O completion object. /// /// /// A TP_CALLBACK_INSTANCE structure that defines the callback instance. Applications do not modify the members of this structure. /// /// The application-defined data. /// /// A pointer to a variable that receives the address of the OVERLAPPED structure that was specified when the completed I/O operation /// was started. /// /// /// The result of the I/O operation. If the I/O is successful, this parameter is NO_ERROR. Otherwise, this parameter is one of the /// system error codes. /// /// The number of bytes transferred during the I/O operation that has completed. /// A TP_IO structure that defines the I/O completion object that generated the callback. [UnmanagedFunctionPointer(CallingConvention.Winapi)] public delegate void IoCompletionCallback(PTP_CALLBACK_INSTANCE Instance, IntPtr Context, IntPtr Overlapped, uint IoResult, UIntPtr NumberOfBytesTransferred, PTP_IO Io); /// Applications implement this callback if they call the TrySubmitThreadpoolCallback function to start a worker thread. /// /// A TP_CALLBACK_INSTANCE structure that defines the callback instance. Applications do not modify the members of this structure. /// /// The application-defined data. [UnmanagedFunctionPointer(CallingConvention.Winapi)] public delegate void SimpleCallback(PTP_CALLBACK_INSTANCE Instance, IntPtr Context); /// /// Applications implement this callback if they call the SetThreadpoolTimer function to start a worker thread for the timer object. /// /// /// A TP_CALLBACK_INSTANCE structure that defines the callback instance. Applications do not modify the members of this structure. /// /// The application-defined data. /// A TP_TIMER structure that defines the timer object that generated the callback. [UnmanagedFunctionPointer(CallingConvention.Winapi)] public delegate void TimerCallback(PTP_CALLBACK_INSTANCE Instance, IntPtr Context, PTP_TIMER Timer); /// /// Applications implement this callback if they call the SetThreadpoolWait function to start a worker thread for the wait object. /// /// /// A TP_CALLBACK_INSTANCE structure that defines the callback instance. Applications do not modify the members of this structure. /// /// The application-defined data. /// A TP_WAIT structure that defines the wait object that generated the callback. /// /// The result of the wait operation. This parameter can be one of the following values from WaitForMultipleObjects: WAIT_OBJECT_0, WAIT_TIMEOUT /// [UnmanagedFunctionPointer(CallingConvention.Winapi)] public delegate void WaitCallback(PTP_CALLBACK_INSTANCE Instance, IntPtr Context, PTP_WAIT Wait, uint WaitResult); /// /// Applications implement this callback if they call the SubmitThreadpoolWork function to start a worker thread for the work object. /// /// /// A TP_CALLBACK_INSTANCE structure that defines the callback instance. Applications do not modify the members of this structure. /// /// The application-defined data. /// A TP_WORK structure that defines the work object that generated the callback. [UnmanagedFunctionPointer(CallingConvention.Winapi)] public delegate void WorkCallback(PTP_CALLBACK_INSTANCE Instance, IntPtr Context, PTP_WORK Work); /// [Flags] public enum TP_CALLBACK_ENV_FLAGS { /// None = 0, /// LongFunction = 1, /// Persistent = 2, } /// The priority for the callback relative to other callbacks in the same thread pool. public enum TP_CALLBACK_PRIORITY { /// The callback should run at high priority. TP_CALLBACK_PRIORITY_HIGH, /// The callback should run at normal priority. TP_CALLBACK_PRIORITY_NORMAL, /// The callback should run at low priority. TP_CALLBACK_PRIORITY_LOW, /// The callback is invalid. TP_CALLBACK_PRIORITY_INVALID, } /// Indicates that the callback may not return quickly. /// /// A TP_CALLBACK_INSTANCE structure that defines the callback instance. The structure is passed to the callback function. /// /// /// /// The function returns TRUE if another thread in the thread pool is available for processing callbacks or the thread pool was able /// to spin up a new thread. In this case, the current callback function may use the current thread indefinitely. /// /// /// The function returns FALSE if another thread in the thread pool is not available to process callbacks and the thread pool was not /// able to spin up a new thread. The thread pool will attempt to spin up a new thread after a delay, but if the current callback /// function runs long, the thread pool may lose efficiency. /// /// // BOOL WINAPI CallbackMayRunLong( _Inout_ PTP_CALLBACK_INSTANCE pci); https://msdn.microsoft.com/en-us/library/windows/desktop/ms681981(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms681981")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool CallbackMayRunLong(PTP_CALLBACK_INSTANCE pci); /// Cancels the notification from the StartThreadpoolIo function. /// /// A TP_IO structure that defines the I/O completion object. The CreateThreadpoolIo function returns this structure. /// /// This function does not return a value. // VOID WINAPI CancelThreadpoolIo( _Inout_ PTP_IO pio); https://msdn.microsoft.com/en-us/library/windows/desktop/ms681983(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms681983")] public static extern void CancelThreadpoolIo(PTP_IO pio); /// Closes the specified thread pool. /// /// A TP_POOL structure that defines the thread pool. The CreateThreadpool function returns this structure. /// /// This function does not return a value. // VOID WINAPI CloseThreadpool( _Inout_ PTP_POOL ptpp); https://msdn.microsoft.com/en-us/library/windows/desktop/ms682030(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms682030")] public static extern void CloseThreadpool(PTP_POOL ptpp); /// Closes the specified cleanup group. /// /// A TP_CLEANUP_GROUP structure that defines the cleanup group. The CreateThreadpoolCleanupGroup returns this structure. /// /// This function does not return a value. // VOID WINAPI CloseThreadpoolCleanupGroup( _Inout_ PTP_CLEANUP_GROUP ptpcg); https://msdn.microsoft.com/en-us/library/windows/desktop/ms682033(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms682033")] public static extern void CloseThreadpoolCleanupGroup(PTP_CLEANUP_GROUP ptpcg); /// /// Releases the members of the specified cleanup group, waits for all callback functions to complete, and optionally cancels any /// outstanding callback functions. /// /// /// A TP_CLEANUP_GROUP structure that defines the cleanup group. The CreateThreadpoolCleanupGroup function returns this structure. /// /// /// If this parameter is TRUE, the function cancels outstanding callbacks that have not yet started. If this parameter is FALSE, the /// function waits for outstanding callback functions to complete. /// /// /// The application-defined data to pass to the application's cleanup group callback function. You can specify the callback function /// when you call SetThreadpoolCallbackCleanupGroup. /// /// This function does not return a value. // VOID WINAPI CloseThreadpoolCleanupGroupMembers( _Inout_ PTP_CLEANUP_GROUP ptpcg, _In_ BOOL fCancelPendingCallbacks, _Inout_opt_ // PVOID pvCleanupContext); https://msdn.microsoft.com/en-us/library/windows/desktop/ms682036(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms682036")] public static extern void CloseThreadpoolCleanupGroupMembers(PTP_CLEANUP_GROUP ptpcg, [MarshalAs(UnmanagedType.Bool)] bool fCancelPendingCallbacks, [Optional] IntPtr pvCleanupContext); /// Releases the specified I/O completion object. /// /// A TP_IO structure that defines the I/O completion object. The CreateThreadpoolIo function returns this structure. /// /// This function does not return a value. // VOID WINAPI CloseThreadpoolIo( _Inout_ PTP_IO pio); https://msdn.microsoft.com/en-us/library/windows/desktop/ms682038(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms682038")] public static extern void CloseThreadpoolIo(PTP_IO pio); /// Releases the specified timer object. /// /// A TP_TIMER structure that defines the timer object. The CreateThreadpoolTimer function returns this structure. /// /// This function does not return a value. // VOID WINAPI CloseThreadpoolTimer( _Inout_ PTP_TIMER pti); https://msdn.microsoft.com/en-us/library/windows/desktop/ms682040(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms682040")] public static extern void CloseThreadpoolTimer(PTP_TIMER pti); /// Releases the specified wait object. /// /// A TP_WAIT structure that defines the wait object. The CreateThreadpoolWait function returns this structure. /// /// This function does not return a value. // VOID WINAPI CloseThreadpoolWait( _Inout_ PTP_WAIT pwa); https://msdn.microsoft.com/en-us/library/windows/desktop/ms682042(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms682042")] public static extern void CloseThreadpoolWait(PTP_WAIT pwa); /// Releases the specified work object. /// /// A TP_WORK structure that defines the work object. The CreateThreadpoolWork function returns this structure. /// /// This function does not return a value. // VOID WINAPI CloseThreadpoolWork( _Inout_ PTP_WORK pwk); https://msdn.microsoft.com/en-us/library/windows/desktop/ms682043(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms682043")] public static extern void CloseThreadpoolWork(PTP_WORK pwk); /// Allocates a new pool of threads to execute callbacks. /// This parameter is reserved and must be NULL. /// /// /// If the function succeeds, it returns a TP_POOL structure representing the newly allocated thread pool. Applications do not /// modify the members of this structure. /// /// If function fails, it returns NULL. To retrieve extended error information, call GetLastError. /// /// /// /// After creating the new thread pool, you should call SetThreadpoolThreadMaximum to specify the maximum number of threads that the /// pool can allocate and SetThreadpoolThreadMinimum to specify the minimum number of threads available in the pool. /// /// /// To use the pool, you must associate the pool with a callback environment. To create the callback environment, call /// InitializeThreadpoolEnvironment. Then, call SetThreadpoolCallbackPool to associate the pool with the callback environment. /// /// To release the thread pool, call CloseThreadpool. /// To compile an application that uses this function, define _WIN32_WINNT as 0x0600 or higher. /// Examples /// For an example, see Using the Thread Pool Functions. /// // https://docs.microsoft.com/en-us/windows/win32/api/threadpoolapiset/nf-threadpoolapiset-createthreadpool PTP_POOL // CreateThreadpool( PVOID reserved ); [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] [PInvokeData("threadpoolapiset.h", MSDNShortId = "cc00d7bf-ac52-44ff-a6a8-76c8eaace5e6")] // public static extern PTP_POOL CreateThreadpool(IntPtr reserved); public static extern SafePTP_POOL CreateThreadpool(IntPtr reserved = default); /// Creates a cleanup group that applications can use to track one or more thread pool callbacks. /// /// /// If the function succeeds, it returns a TP_CLEANUP_GROUP structure of the newly allocated cleanup group. Applications do /// not modify the members of this structure. /// /// If function fails, it returns NULL. To retrieve extended error information, call GetLastError. /// // PTP_CLEANUP_GROUP WINAPI CreateThreadpoolCleanupGroup(void); https://msdn.microsoft.com/en-us/library/windows/desktop/ms682462(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms682462")] public static extern SafePTP_CLEANUP_GROUP CreateThreadpoolCleanupGroup(); /// Creates a new I/O completion object. /// The file handle to bind to this I/O completion object. /// /// The callback function to be called each time an overlapped I/O operation completes on the file. For details, see IoCompletionCallback. /// /// Optional application-defined data to pass to the callback function. /// /// /// A TP_CALLBACK_ENVIRON structure that defines the environment in which to execute the callback. The /// InitializeThreadpoolEnvironment function returns this structure. /// /// If this parameter is NULL, the callback executes in the default callback environment. For more information, see InitializeThreadpoolEnvironment. /// /// /// /// If the function succeeds, it returns a TP_IO structure that defines the I/O object. Applications do not modify the members /// of this structure. /// /// If the function fails, it returns NULL. To retrieve extended error information, call GetLastError. /// // PTP_IO WINAPI CreateThreadpoolIo( _In_ HANDLE fl, _In_ IoCompletionCallback pfnio, _Inout_opt_ PVOID pv, _In_opt_ // PTP_CALLBACK_ENVIRON pcbe); https://msdn.microsoft.com/en-us/library/windows/desktop/ms682464(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms682464")] public static extern SafePTP_IO CreateThreadpoolIo(HFILE fl, IoCompletionCallback pfnio, IntPtr pv, PTP_CALLBACK_ENVIRON pcbe); /// Creates a new timer object. /// The callback function to call each time the timer object expires. For details, see TimerCallback. /// Optional application-defined data to pass to the callback function. /// /// /// A TP_CALLBACK_ENVIRON structure that defines the environment in which to execute the callback. The /// InitializeThreadpoolEnvironment function returns this structure. /// /// If this parameter is NULL, the callback executes in the default callback environment. For more information, see InitializeThreadpoolEnvironment. /// /// /// /// If the function succeeds, it returns a TP_TIMER structure that defines the timer object. Applications do not modify the /// members of this structure. /// /// If the function fails, it returns NULL. To retrieve extended error information, call GetLastError. /// // PTP_TIMER WINAPI CreateThreadpoolTimer( _In_ TimerCallback pfnti, _Inout_opt_ PVOID pv, _In_opt_ PTP_CALLBACK_ENVIRON pcbe); https://msdn.microsoft.com/en-us/library/windows/desktop/ms682466(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms682466")] public static extern SafePTP_TIMER CreateThreadpoolTimer(TimerCallback pfnti, [Optional] IntPtr pv, [Optional] PTP_CALLBACK_ENVIRON pcbe); /// Creates a new wait object. /// The callback function to call when the wait completes or times out. For details, see WaitCallback. /// Optional application-defined data to pass to the callback function. /// /// /// A TP_CALLBACK_ENVIRON structure that defines the environment in which to execute the callback. The /// InitializeThreadpoolEnvironment function returns this structure. /// /// If this parameter is NULL, the callback executes in the default callback environment. For more information, see InitializeThreadpoolEnvironment. /// /// /// /// If the function succeeds, it returns a TP_WAIT structure that defines the wait object. Applications do not modify the /// members of this structure. /// /// If the function fails, it returns NULL. To retrieve extended error information, call GetLastError. /// // PTP_WAIT WINAPI CreateThreadpoolWait( _In_ WaitCallback pfnwa, _Inout_opt_ PVOID pv, _In_opt_ PTP_CALLBACK_ENVIRON pcbe); https://msdn.microsoft.com/en-us/library/windows/desktop/ms682474(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms682474")] public static extern SafePTP_WAIT CreateThreadpoolWait(WaitCallback pfnwa, [Optional] IntPtr pv, [Optional] PTP_CALLBACK_ENVIRON pcbe); /// Creates a new work object. /// /// The callback function. A worker thread calls this callback each time you call SubmitThreadpoolWork to post the work /// object. For details, see WorkCallback. /// /// Optional application-defined data to pass to the callback function. /// /// /// A TP_CALLBACK_ENVIRON structure that defines the environment in which to execute the callback. The /// InitializeThreadpoolEnvironment function returns this structure. /// /// If this parameter is NULL, the callback executes in the default callback environment. For more information, see InitializeThreadpoolEnvironment. /// /// /// /// If the function succeeds, it returns a TP_WORK structure that defines the work object. Applications do not modify the /// members of this structure. /// /// If the function fails, it returns NULL. To retrieve extended error information, call GetLastError. /// // PTP_WORK WINAPI CreateThreadpoolWork( _In_ WorkCallback pfnwk, _Inout_opt_ PVOID pv, _In_opt_ PTP_CALLBACK_ENVIRON pcbe); https://msdn.microsoft.com/en-us/library/windows/desktop/ms682478(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms682478")] public static extern SafePTP_WORK CreateThreadpoolWork(WorkCallback pfnwk, [Optional] IntPtr pv, [Optional] PTP_CALLBACK_ENVIRON pcbe); /// /// Removes the association between the currently executing callback function and the object that initiated the callback. The current /// thread will no longer count as executing a callback on behalf of the object. /// /// /// A TP_CALLBACK_INSTANCE structure that defines the callback instance. The structure is passed to the callback function. /// /// This function does not return a value. // VOID WINAPI DisassociateCurrentThreadFromCallback( _Inout_ PTP_CALLBACK_INSTANCE pci); https://msdn.microsoft.com/en-us/library/windows/desktop/ms682581(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms682581")] public static extern void DisassociateCurrentThreadFromCallback(PTP_CALLBACK_INSTANCE pci); /// Specifies the DLL that the thread pool will unload when the current callback completes. /// /// A TP_CALLBACK_INSTANCE structure that defines the callback instance. The structure is passed to the callback function. /// /// A handle to the DLL. /// This function does not return a value. // VOID WINAPI FreeLibraryWhenCallbackReturns( _Inout_ PTP_CALLBACK_INSTANCE pci, _In_ HMODULE mod); https://msdn.microsoft.com/en-us/library/windows/desktop/ms683154(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms683154")] public static extern void FreeLibraryWhenCallbackReturns(PTP_CALLBACK_INSTANCE pci, HINSTANCE mod); /// Initializes a callback environment. /// A TP_CALLBACK_ENVIRON structure that defines a callback environment. /// This function does not return a value. // VOID InitializeThreadpoolEnvironment( _Out_ PTP_CALLBACK_ENVIRON pcbe); https://msdn.microsoft.com/en-us/library/windows/desktop/ms683486(v=vs.85).aspx [PInvokeData("WinBase.h", MSDNShortId = "ms683486")] public static void InitializeThreadpoolEnvironment(out PTP_CALLBACK_ENVIRON pcbe) => pcbe = new PTP_CALLBACK_ENVIRON(); /// Determines whether the specified timer object is currently set. /// /// A TP_TIMER structure that defines the timer object. The CreateThreadpoolTimer function returns this structure. /// /// The return value is TRUE if the timer is set; otherwise, the return value is FALSE. // BOOL WINAPI IsThreadpoolTimerSet( _Inout_ PTP_TIMER pti); https://msdn.microsoft.com/en-us/library/windows/desktop/ms684133(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms684133")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool IsThreadpoolTimerSet(PTP_TIMER pti); /// Specifies the critical section that the thread pool will release when the current callback completes. /// /// A TP_CALLBACK_INSTANCE structure that defines the callback instance. The structure is passed to the callback function. /// /// The critical section. /// This function does not return a value. // VOID WINAPI LeaveCriticalSectionWhenCallbackReturns( _Inout_ PTP_CALLBACK_INSTANCE pci, _Inout_ PCRITICAL_SECTION pcs); https://msdn.microsoft.com/en-us/library/windows/desktop/ms684171(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms684171")] public static extern void LeaveCriticalSectionWhenCallbackReturns(PTP_CALLBACK_INSTANCE pci, ref CRITICAL_SECTION pcs); /// Retrieves the stack reserve and commit sizes for threads in the specified thread pool. /// /// A TP_POOL structure that specifies the thread pool. The CreateThreadpool function returns this structure. /// /// A TP_POOL_STACK_INFORMATION structure that receives the stack reserve and commit size. /// /// If the function succeeds, the return value is nonzero. /// If the function fails, the return value is zero. To get extended error information, call GetLastError. /// // BOOL QueryThreadpoolStackInformation( _In_ PTP_POOL ptpp, _Out_ PTP_POOL_STACK_INFORMATION ptpsi); https://msdn.microsoft.com/en-us/library/windows/desktop/dd405508(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "dd405508")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool QueryThreadpoolStackInformation(PTP_POOL ptpp, out TP_POOL_STACK_INFORMATION ptpsi); /// Specifies the mutex that the thread pool will release when the current callback completes. /// /// A TP_CALLBACK_INSTANCE structure that defines the callback instance. The structure is passed to the callback function. /// /// A handle to the mutex. /// This function does not return a value. // VOID WINAPI ReleaseMutexWhenCallbackReturns( _Inout_ PTP_CALLBACK_INSTANCE pci, _In_ HANDLE mut); https://msdn.microsoft.com/en-us/library/windows/desktop/ms685070(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms685070")] public static extern void ReleaseMutexWhenCallbackReturns(PTP_CALLBACK_INSTANCE pci, SafeMutexHandle mut); /// Specifies the semaphore that the thread pool will release when the current callback completes. /// /// A TP_CALLBACK_INSTANCE structure that defines the callback instance. The structure is passed to the callback function. /// /// A handle to the semaphore. /// The amount by which to increment the semaphore object's count. /// This function does not return a value. // VOID WINAPI ReleaseSemaphoreWhenCallbackReturns( _Inout_ PTP_CALLBACK_INSTANCE pci, _In_ HANDLE sem, _In_ DWORD crel); https://msdn.microsoft.com/en-us/library/windows/desktop/ms685073(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms685073")] public static extern void ReleaseSemaphoreWhenCallbackReturns(PTP_CALLBACK_INSTANCE pci, SafeSemaphoreHandle sem, uint crel); /// Specifies the event that the thread pool will set when the current callback completes. /// /// A TP_CALLBACK_INSTANCE structure that defines the callback instance. The structure is passed to the callback function. /// /// A handle to the event to be set. /// This function does not return a value. // VOID WINAPI SetEventWhenCallbackReturns( _Inout_ PTP_CALLBACK_INSTANCE pci, _In_ HANDLE evt); https://msdn.microsoft.com/en-us/library/windows/desktop/ms686214(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms686214")] public static extern void SetEventWhenCallbackReturns(PTP_CALLBACK_INSTANCE pci, SafeEventHandle evt); /// Associates the specified cleanup group with the specified callback environment. /// /// A TP_CALLBACK_ENVIRON structure that defines the callback environment. The InitializeThreadpoolEnvironment function /// returns this structure. /// /// /// A TP_CLEANUP_GROUP structure that defines the cleanup group. The CreateThreadpoolCleanupGroup function returns this structure. /// /// /// The cleanup callback to be called if the cleanup group is canceled before the associated object is released. The function is /// called when you call CloseThreadpoolCleanupGroupMembers. /// /// This function does not return a value. // VOID SetThreadpoolCallbackCleanupGroup( _Inout_ PTP_CALLBACK_ENVIRON pcbe, _In_ PTP_CLEANUP_GROUP ptpcg, _In_opt_ // PTP_CLEANUP_GROUP_CANCEL_CALLBACK pfng); https://msdn.microsoft.com/en-us/library/windows/desktop/ms686255(v=vs.85).aspx [PInvokeData("WinBase.h", MSDNShortId = "ms686255")] public static void SetThreadpoolCallbackCleanupGroup(this PTP_CALLBACK_ENVIRON pcbe, PTP_CLEANUP_GROUP ptpcg, [Optional] CleanupGroupCancelCallback pfng) { pcbe.CleanupGroup = ptpcg; pcbe.CleanupGroupCancelCallback = pfng; } /// Ensures that the specified DLL remains loaded as long as there are outstanding callbacks. /// /// A TP_CALLBACK_ENVIRON structure that defines the callback environment. The InitializeThreadpoolEnvironment function /// returns this structure. /// /// A handle to the DLL. /// This function does not return a value. // VOID SetThreadpoolCallbackLibrary( _Inout_ PTP_CALLBACK_ENVIRON pcbe, _In_ PVOID mod); https://msdn.microsoft.com/en-us/library/windows/desktop/ms686258(v=vs.85).aspx [PInvokeData("WinBase.h", MSDNShortId = "ms686258")] public static void SetThreadpoolCallbackLibrary(this PTP_CALLBACK_ENVIRON pcbe, HINSTANCE mod) => pcbe.RaceDll = mod; /// Specifies that the callback should run on a persistent thread. /// /// A TP_CALLBACK_ENVIRON structure that defines the callback environment. The InitializeThreadpoolEnvironment function /// returns this structure. /// /// This function does not return a value. // VOID SetThreadpoolCallbackPersistent( _Inout_ PTP_CALLBACK_ENVIRON pcbe); https://msdn.microsoft.com/en-us/library/windows/desktop/dd405518(v=vs.85).aspx [PInvokeData("WinBase.h", MSDNShortId = "dd405518")] public static void SetThreadpoolCallbackPersistent(this PTP_CALLBACK_ENVIRON pcbe) => pcbe.Flags |= TP_CALLBACK_ENV_FLAGS.Persistent; /// Sets the thread pool to be used when generating callbacks. /// /// A TP_CALLBACK_ENVIRON structure that defines the callback environment. The InitializeThreadpoolEnvironment function /// returns this structure. /// /// /// A TP_POOL structure that defines the thread pool. The CreateThreadpool function returns this structure. /// /// This function does not return a value. // VOID SetThreadpoolCallbackPool( _Inout_ PTP_CALLBACK_ENVIRON pcbe, _In_ PTP_POOL ptpp); https://msdn.microsoft.com/en-us/library/windows/desktop/ms686261(v=vs.85).aspx [PInvokeData("WinBase.h", MSDNShortId = "ms686261")] public static void SetThreadpoolCallbackPool(this PTP_CALLBACK_ENVIRON pcbe, PTP_POOL ptpp) => pcbe.Pool = ptpp; /// Specifies the priority of a callback function relative to other work items in the same thread pool. /// /// A TP_CALLBACK_ENVIRON structure that defines the callback environment. The InitializeThreadpoolEnvironment function /// returns this structure. /// /// /// /// The priority for the callback relative to other callbacks in the same thread pool. This parameter can be one of the following /// TP_CALLBACK_PRIORITY enumeration values: /// /// /// /// /// Value /// Meaning /// /// /// TP_CALLBACK_PRIORITY_HIGH /// The callback should run at high priority. /// /// /// TP_CALLBACK_PRIORITY_LOW /// The callback should run at low priority. /// /// /// TP_CALLBACK_PRIORITY_NORMAL /// The callback should run at normal priority. /// /// /// /// /// This function does not return a value. // VOID SetThreadpoolCallbackPriority( _Inout_ PTP_CALLBACK_ENVIRON pcbe, _In_ TP_CALLBACK_PRIORITY Priority); https://msdn.microsoft.com/en-us/library/windows/desktop/dd405519(v=vs.85).aspx [PInvokeData("WinBase.h", MSDNShortId = "dd405519")] public static void SetThreadpoolCallbackPriority(this PTP_CALLBACK_ENVIRON pcbe, TP_CALLBACK_PRIORITY Priority) => pcbe.CallbackPriority = Priority; /// Indicates that callbacks associated with this callback environment may not return quickly. /// /// A TP_CALLBACK_ENVIRON structure that defines the callback environment. The InitializeThreadpoolEnvironment function /// returns this structure. /// /// This function does not return a value. // VOID SetThreadpoolCallbackRunsLong( _Inout_ PTP_CALLBACK_ENVIRON pcbe); https://msdn.microsoft.com/en-us/library/windows/desktop/ms686263(v=vs.85).aspx [PInvokeData("WinBase.h", MSDNShortId = "ms686263")] public static void SetThreadpoolCallbackRunsLong(this PTP_CALLBACK_ENVIRON pcbe) => pcbe.Flags |= TP_CALLBACK_ENV_FLAGS.LongFunction; /// /// Sets the stack reserve and commit sizes for new threads in the specified thread pool. Stack reserve and commit sizes for existing /// threads are not changed. /// /// /// A TP_POOL structure that specifies the thread pool. The CreateThreadpool function returns this structure. /// /// /// A TP_POOL_STACK_INFORMATION structure that specifies the stack reserve and commit size for threads in the pool. /// /// /// If the function succeeds, the return value is nonzero. /// If the function fails, the return value is zero. To get extended error information, call GetLastError. /// // BOOL SetThreadpoolStackInformation( _Inout_ PTP_POOL ptpp, _In_ PTP_POOL_STACK_INFORMATION ptpsi); https://msdn.microsoft.com/en-us/library/windows/desktop/dd405520(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "dd405520")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SetThreadpoolStackInformation(PTP_POOL ptpp, in TP_POOL_STACK_INFORMATION ptpsi); /// Sets the maximum number of threads that the specified thread pool can allocate to process callbacks. /// /// A TP_POOL structure that defines the thread pool. The CreateThreadpool function returns this structure. /// /// The maximum number of threads. /// This function does not return a value. // VOID WINAPI SetThreadpoolThreadMaximum( _Inout_ PTP_POOL ptpp, _In_ DWORD cthrdMost); https://msdn.microsoft.com/en-us/library/windows/desktop/ms686266(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms686266")] public static extern void SetThreadpoolThreadMaximum(PTP_POOL ptpp, uint cthrdMost); /// Sets the minimum number of threads that the specified thread pool must make available to process callbacks. /// /// A TP_POOL structure that defines the thread pool. The CreateThreadpool function returns this structure. /// /// The minimum number of threads. /// /// If the function succeeds, it returns TRUE. /// If the function fails, it returns FALSE. To retrieve extended error information, call GetLastError. /// // BOOL WINAPI SetThreadpoolThreadMinimum( _Inout_ PTP_POOL ptpp, _In_ DWORD cthrdMic); https://msdn.microsoft.com/en-us/library/windows/desktop/ms686268(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms686268")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SetThreadpoolThreadMinimum(PTP_POOL ptpp, uint cthrdMic); /// /// Sets the timer object—, replacing the previous timer, if any. A worker thread calls the timer object's callback after the /// specified timeout expires. /// /// /// A pointer to a TP_TIMER structure that defines the timer object to set. The CreateThreadpoolTimer function returns /// this structure. /// /// /// /// A pointer to a FILETIME structure that specifies the absolute or relative time at which the timer should expire. If /// positive or zero, it indicates the absolute time since January 1, 1601 (UTC), measured in 100 nanosecond units. If negative, it /// indicates the amount of time to wait relative to the current time. For more information about time values, see File Times. /// /// /// If this parameter is NULL, the timer object will cease to queue new callbacks (but callbacks already queued will still occur). /// Note that if this parameter is zero, the timer will expire immediately. /// /// /// /// The timer period, in milliseconds. If this parameter is zero, the timer is signaled once. If this parameter is greater than zero, /// the timer is periodic. A periodic timer automatically reactivates each time the period elapses, until the timer is canceled. /// /// /// The maximum amount of time the system can delay before calling the timer callback. If this parameter is set, the system can batch /// calls to conserve power. /// /// This function does not return a value. // VOID WINAPI SetThreadpoolTimer( _Inout_ PTP_TIMER pti, _In_opt_ PFILETIME pftDueTime, _In_ DWORD msPeriod, _In_opt_ DWORD // msWindowLength); https://msdn.microsoft.com/en-us/library/windows/desktop/ms686271(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms686271")] public static extern void SetThreadpoolTimer(PTP_TIMER pti, in FILETIME pftDueTime, [Optional] uint msPeriod, [Optional] uint msWindowLength); /// /// Sets the timer object—, replacing the previous timer, if any. A worker thread calls the timer object's callback after the /// specified timeout expires. /// /// /// A pointer to a TP_TIMER structure that defines the timer object to set. The CreateThreadpoolTimer function returns /// this structure. /// /// /// /// A pointer to a FILETIME structure that specifies the absolute or relative time at which the timer should expire. If /// positive or zero, it indicates the absolute time since January 1, 1601 (UTC), measured in 100 nanosecond units. If negative, it /// indicates the amount of time to wait relative to the current time. For more information about time values, see File Times. /// /// /// If this parameter is NULL, the timer object will cease to queue new callbacks (but callbacks already queued will still occur). /// Note that if this parameter is zero, the timer will expire immediately. /// /// /// /// The timer period, in milliseconds. If this parameter is zero, the timer is signaled once. If this parameter is greater than zero, /// the timer is periodic. A periodic timer automatically reactivates each time the period elapses, until the timer is canceled. /// /// /// The maximum amount of time the system can delay before calling the timer callback. If this parameter is set, the system can batch /// calls to conserve power. /// /// /// If the timer was previously active and was canceled, a value of TRUE is returned. Otherwise a value of FALSE is returned. If /// FALSE is returned, a callback may be in progress or about to commence. If this is the case, a subsequent SetThreadpoolTimerEx /// operation will be properly synchronized with completion of the timer callback. /// // https://msdn.microsoft.com/en-us/library/windows/desktop/dn894018(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("Threadpoolapiset.h", MSDNShortId = "dn894018")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SetThreadpoolTimerEx(PTP_TIMER pti, in FILETIME pftDueTime, [Optional] uint msPeriod, [Optional] uint msWindowLength); /// /// Sets the wait object—replacing the previous wait object, if any. A worker thread calls the wait object's callback function after /// the handle becomes signaled or after the specified timeout expires. /// /// /// A pointer to a TP_WAIT structure that defines the wait object. The CreateThreadpoolWait function returns this structure. /// /// /// A handle. /// /// If this parameter is NULL, the wait object will cease to queue new callbacks (but callbacks already queued will still occur). /// /// If this parameter is not NULL, it must refer to a valid waitable object. /// /// If this handle is closed while the wait is still pending, the function's behavior is undefined. If the wait is still pending and /// the handle must be closed, use CloseThreadpoolWait to cancel the wait and then close the handle. /// /// /// /// /// A pointer to a FILETIME structure that specifies the absolute or relative time at which the wait operation should time /// out. If this parameter points to a positive value, it indicates the absolute time since January 1, 1601 (UTC), in 100-nanosecond /// intervals. If this parameter points to a negative value, it indicates the amount of time to wait relative to the current time. /// For more information about time values, see File Times. /// /// If this parameter points to 0, the wait times out immediately. If this parameter is NULL, the wait will not time out. /// /// This function does not return a value. // VOID WINAPI SetThreadpoolWait( _Inout_ PTP_WAIT pwa, _In_opt_ HANDLE h, _In_opt_ PFILETIME pftTimeout); https://msdn.microsoft.com/en-us/library/windows/desktop/ms686273(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms686273")] public static extern void SetThreadpoolWait(PTP_WAIT pwa, SafeEventHandle h, in FILETIME pftTimeout); /// /// Sets the wait object—replacing the previous wait object, if any. A worker thread calls the wait object's callback function after /// the handle becomes signaled or after the specified timeout expires. /// /// /// A pointer to a TP_WAIT structure that defines the wait object. The CreateThreadpoolWait function returns this structure. /// /// /// A handle. /// /// If this parameter is NULL, the wait object will cease to queue new callbacks (but callbacks already queued will still occur). /// /// If this parameter is not NULL, it must refer to a valid waitable object. /// /// If this handle is closed while the wait is still pending, the function's behavior is undefined. If the wait is still pending and /// the handle must be closed, use CloseThreadpoolWait to cancel the wait and then close the handle. /// /// /// /// /// A pointer to a FILETIME structure that specifies the absolute or relative time at which the wait operation should time /// out. If this parameter points to a positive value, it indicates the absolute time since January 1, 1601 (UTC), in 100-nanosecond /// intervals. If this parameter points to a negative value, it indicates the amount of time to wait relative to the current time. /// For more information about time values, see File Times. /// /// If this parameter points to 0, the wait times out immediately. If this parameter is NULL, the wait will not time out. /// /// This function does not return a value. // VOID WINAPI SetThreadpoolWait( _Inout_ PTP_WAIT pwa, _In_opt_ HANDLE h, _In_opt_ PFILETIME pftTimeout); https://msdn.microsoft.com/en-us/library/windows/desktop/ms686273(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms686273")] public static extern void SetThreadpoolWait(PTP_WAIT pwa, SafeEventHandle h, [Optional] IntPtr pftTimeout); /// /// Sets the wait object—replacing the previous wait object, if any. A worker thread calls the wait object's callback function after /// the handle becomes signaled or after the specified timeout expires. /// /// /// A pointer to a TP_WAIT structure that defines the wait object. The CreateThreadpoolWait function returns this structure. /// /// /// A handle. /// /// If this parameter is NULL, the wait object will cease to queue new callbacks (but callbacks already queued will still occur). /// /// If this parameter is not NULL, it must refer to a valid waitable object. /// /// If this handle is closed while the wait is still pending, the function's behavior is undefined. If the wait is still pending and /// the handle must be closed, use CloseThreadpoolWait to cancel the wait and then close the handle. /// /// /// /// /// A pointer to a FILETIME structure that specifies the absolute or relative time at which the wait operation should time /// out. If this parameter points to a positive value, it indicates the absolute time since January 1, 1601 (UTC), in 100-nanosecond /// intervals. If this parameter points to a negative value, it indicates the amount of time to wait relative to the current time. /// For more information about time values, see File Times. /// /// If this parameter points to 0, the wait times out immediately. If this parameter is NULL, the wait will not time out. /// /// This function does not return a value. // VOID WINAPI SetThreadpoolWait( _Inout_ PTP_WAIT pwa, _In_opt_ HANDLE h, _In_opt_ PFILETIME pftTimeout); https://msdn.microsoft.com/en-us/library/windows/desktop/ms686273(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms686273")] public static extern void SetThreadpoolWait(PTP_WAIT pwa, [Optional] IntPtr h, [Optional] IntPtr pftTimeout); /// /// Sets the wait object—replacing the previous wait object, if any. A worker thread calls the wait object's callback function after /// the handle becomes signaled or after the specified timeout expires. /// /// /// A pointer to a TP_WAIT structure that defines the wait object. The CreateThreadpoolWait function returns this structure. /// /// /// A handle. /// /// If this parameter is NULL, the wait object will cease to queue new callbacks (but callbacks already queued will still occur). /// /// If this parameter is not NULL, it must refer to a valid waitable object. /// /// If this handle is closed while the wait is still pending, the function's behavior is undefined. If the wait is still pending and /// the handle must be closed, use CloseThreadpoolWait to cancel the wait and then close the handle. /// /// /// /// A pointer to a FILETIME structure that specifies the absolute or relative time at which the wait operation should time /// out. If this parameter points to a positive value, it indicates the absolute time since January 1, 1601 (UTC), in 100-nanosecond /// intervals. If this parameter points to a negative value, it indicates the amount of time to wait relative to the current time. /// For more information about time values, see File Times. /// /// Reserved. /// /// TRUE, if the timer was previously active and was canceled; otherwise, FALSE. This return value can be used to maintain reference /// counts to synchronize between completion and cancellation of a non-periodic timer operation. If FALSE is returned, a callback may /// be in progress or about to commence. If FALSE is returned, a subsequent WaitForThreadpool or WaitForThreadpoolEx /// Callback operation will complete after that callback is completed. A subsequent SetThreadpool or SetThreadpoolEx /// operation that is not later canceled will result in an additional callback.” /// // BOOL WINAPI SetThreadpoolWaitEx( _Inout_ PTP_WAIT pwa, _In_opt_ HANDLE h, _In_opt_ PFILETIME pftTimeout, _Reserved_ PVOID // Reserved); https://msdn.microsoft.com/en-us/library/windows/desktop/mt186618(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("Threadpoolapiset.h", MSDNShortId = "mt186618")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SetThreadpoolWaitEx(PTP_WAIT pwa, IntPtr h, in FILETIME pftTimeout, IntPtr Reserved = default); /// /// Notifies the thread pool that I/O operations may possibly begin for the specified I/O completion object. A worker thread calls /// the I/O completion object's callback function after the operation completes on the file handle bound to this object. /// /// /// A TP_IO structure that defines the I/O completion object. The CreateThreadpoolIo function returns this structure. /// /// This function does not return a value. // VOID WINAPI StartThreadpoolIo( _Inout_ PTP_IO pio); https://msdn.microsoft.com/en-us/library/windows/desktop/ms686326(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms686326")] public static extern void StartThreadpoolIo(PTP_IO pio); /// Posts a work object to the thread pool. A worker thread calls the work object's callback function. /// /// A TP_WORK structure that defines the work object. The CreateThreadpoolWork function returns this structure. /// /// This function does not return a value. // VOID WINAPI SubmitThreadpoolWork( _Inout_ PTP_WORK pwk); https://msdn.microsoft.com/en-us/library/windows/desktop/ms686338(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms686338")] public static extern void SubmitThreadpoolWork(PTP_WORK pwk); /// Requests that a thread pool worker thread call the specified callback function. /// The callback function. For details, see SimpleCallback. /// Optional application-defined data to pass to the callback function. /// /// /// A TP_CALLBACK_ENVIRON structure that defines the environment in which to execute the callback function. The /// InitializeThreadpoolEnvironment function returns this structure. /// /// If this parameter is NULL, the callback executes in the default callback environment. For more information, see InitializeThreadpoolEnvironment. /// /// /// If the function succeeds, it returns TRUE. /// If the function fails, it returns FALSE. To retrieve extended error information, call GetLastError. /// // BOOL WINAPI TrySubmitThreadpoolCallback( _In_ SimpleCallback pfns, _Inout_opt_ PVOID pv, _In_opt_ PTP_CALLBACK_ENVIRON pcbe); https://msdn.microsoft.com/en-us/library/windows/desktop/ms686862(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms686862")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool TrySubmitThreadpoolCallback(SimpleCallback pfns, [Optional] IntPtr pv, [Optional] PTP_CALLBACK_ENVIRON pcbe); /// /// Waits for outstanding I/O completion callbacks to complete and optionally cancels pending callbacks that have not yet started to execute. /// /// /// A TP_IO structure that defines the I/O completion object. The CreateThreadpoolIo function returns this structure. /// /// Indicates whether to cancel queued callbacks that have not yet started to execute. /// This function does not return a value. // VOID WINAPI WaitForThreadpoolIoCallbacks( _Inout_ PTP_IO pio, _In_ BOOL fCancelPendingCallbacks); https://msdn.microsoft.com/en-us/library/windows/desktop/ms687038(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms687038")] public static extern void WaitForThreadpoolIoCallbacks(PTP_IO pio, [MarshalAs(UnmanagedType.Bool)] bool fCancelPendingCallbacks); /// /// Waits for outstanding timer callbacks to complete and optionally cancels pending callbacks that have not yet started to execute. /// /// /// A TP_TIMER structure that defines the timer object. The CreateThreadpoolTimer function returns the TP_TIMER structure. /// /// Indicates whether to cancel queued callbacks that have not yet started to execute. /// This function does not return a value. // VOID WINAPI WaitForThreadpoolTimerCallbacks( _Inout_ PTP_TIMER pti, _In_ BOOL fCancelPendingCallbacks); https://msdn.microsoft.com/en-us/library/windows/desktop/ms687042(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms687042")] public static extern void WaitForThreadpoolTimerCallbacks(PTP_TIMER pti, [MarshalAs(UnmanagedType.Bool)] bool fCancelPendingCallbacks); /// /// Waits for outstanding wait callbacks to complete and optionally cancels pending callbacks that have not yet started to execute. /// /// /// A TP_WAIT structure that defines the wait object. The CreateThreadpoolWait function returns the TP_WAIT structure. /// /// Indicates whether to cancel queued callbacks that have not yet started to execute. /// This function does not return a value. // VOID WINAPI WaitForThreadpoolWaitCallbacks( _Inout_ PTP_WAIT pwa, _In_ BOOL fCancelPendingCallbacks); https://msdn.microsoft.com/en-us/library/windows/desktop/ms687047(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms687047")] public static extern void WaitForThreadpoolWaitCallbacks(PTP_WAIT pwa, [MarshalAs(UnmanagedType.Bool)] bool fCancelPendingCallbacks); /// /// Waits for outstanding work callbacks to complete and optionally cancels pending callbacks that have not yet started to execute. /// /// /// A TP_WORK structure that defines the work object. The CreateThreadpoolWork function returns this structure. /// /// Indicates whether to cancel queued callbacks that have not yet started to execute. /// This function does not return a value. // VOID WINAPI WaitForThreadpoolWorkCallbacks( _Inout_ PTP_WORK pwk, _In_ BOOL fCancelPendingCallbacks); https://msdn.microsoft.com/en-us/library/windows/desktop/ms687053(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)] [PInvokeData("WinBase.h", MSDNShortId = "ms687053")] public static extern void WaitForThreadpoolWorkCallbacks(PTP_WORK pwk, [MarshalAs(UnmanagedType.Bool)] bool fCancelPendingCallbacks); /// Creates a new timer object. /// The callback function to call each time the timer object expires. For details, see TimerCallback. /// Optional application-defined data to pass to the callback function. /// /// /// A TP_CALLBACK_ENVIRON structure that defines the environment in which to execute the callback. The /// InitializeThreadpoolEnvironment function returns this structure. /// /// If this parameter is NULL, the callback executes in the default callback environment. For more information, see InitializeThreadpoolEnvironment. /// /// /// /// If the function succeeds, it returns a TP_TIMER structure that defines the timer object. Applications do not modify the /// members of this structure. /// /// If the function fails, it returns NULL. To retrieve extended error information, call GetLastError. /// // PTP_TIMER WINAPI CreateThreadpoolTimer( _In_ TimerCallback pfnti, _Inout_opt_ PVOID pv, _In_opt_ PTP_CALLBACK_ENVIRON pcbe); https://msdn.microsoft.com/en-us/library/windows/desktop/ms682466(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, EntryPoint = "CreateThreadpoolTimer")] [PInvokeData("WinBase.h", MSDNShortId = "ms682466")] internal static extern PTP_TIMER InternalCreateThreadpoolTimer(TimerCallback pfnti, IntPtr pv, PTP_CALLBACK_ENVIRON pcbe); /// Creates a new wait object. /// The callback function to call when the wait completes or times out. For details, see WaitCallback. /// Optional application-defined data to pass to the callback function. /// /// /// A TP_CALLBACK_ENVIRON structure that defines the environment in which to execute the callback. The /// InitializeThreadpoolEnvironment function returns this structure. /// /// If this parameter is NULL, the callback executes in the default callback environment. For more information, see InitializeThreadpoolEnvironment. /// /// /// /// If the function succeeds, it returns a TP_WAIT structure that defines the wait object. Applications do not modify the /// members of this structure. /// /// If the function fails, it returns NULL. To retrieve extended error information, call GetLastError. /// // PTP_WAIT WINAPI CreateThreadpoolWait( _In_ WaitCallback pfnwa, _Inout_opt_ PVOID pv, _In_opt_ PTP_CALLBACK_ENVIRON pcbe); https://msdn.microsoft.com/en-us/library/windows/desktop/ms682474(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, EntryPoint = "CreateThreadpoolWait")] [PInvokeData("WinBase.h", MSDNShortId = "ms682474")] internal static extern PTP_WAIT InternalCreateThreadpoolWait(WaitCallback pfnwa, IntPtr pv, PTP_CALLBACK_ENVIRON pcbe); /// Creates a new work object. /// /// The callback function. A worker thread calls this callback each time you call SubmitThreadpoolWork to post the work /// object. For details, see WorkCallback. /// /// Optional application-defined data to pass to the callback function. /// /// /// A TP_CALLBACK_ENVIRON structure that defines the environment in which to execute the callback. The /// InitializeThreadpoolEnvironment function returns this structure. /// /// If this parameter is NULL, the callback executes in the default callback environment. For more information, see InitializeThreadpoolEnvironment. /// /// /// /// If the function succeeds, it returns a TP_WORK structure that defines the work object. Applications do not modify the /// members of this structure. /// /// If the function fails, it returns NULL. To retrieve extended error information, call GetLastError. /// // PTP_WORK WINAPI CreateThreadpoolWork( _In_ WorkCallback pfnwk, _Inout_opt_ PVOID pv, _In_opt_ PTP_CALLBACK_ENVIRON pcbe); https://msdn.microsoft.com/en-us/library/windows/desktop/ms682478(v=vs.85).aspx [DllImport(Lib.Kernel32, SetLastError = true, EntryPoint = "CreateThreadpoolWork")] [PInvokeData("WinBase.h", MSDNShortId = "ms682478")] internal static extern PTP_WORK InternalCreateThreadpoolWork(WorkCallback pfnwk, IntPtr pv, PTP_CALLBACK_ENVIRON pcbe); /// [StructLayout(LayoutKind.Sequential)] public struct PTP_CALLBACK_INSTANCE { /// private readonly IntPtr handle; } /// Provides a handle to a pool cleanup group. [StructLayout(LayoutKind.Sequential)] public struct PTP_CLEANUP_GROUP : IHandle { private IntPtr handle; /// Initializes a new instance of the struct. /// An object that represents the pre-existing handle to use. public PTP_CLEANUP_GROUP(IntPtr preexistingHandle) => handle = preexistingHandle; /// Returns an invalid handle by instantiating a object with . public static PTP_CLEANUP_GROUP NULL => new PTP_CLEANUP_GROUP(IntPtr.Zero); /// Gets a value indicating whether this instance is a null handle. public bool IsNull => handle == IntPtr.Zero; /// Performs an explicit conversion from to . /// The handle. /// The result of the conversion. public static explicit operator IntPtr(PTP_CLEANUP_GROUP h) => h.handle; /// Performs an implicit conversion from to . /// The pointer to a handle. /// The result of the conversion. public static implicit operator PTP_CLEANUP_GROUP(IntPtr h) => new PTP_CLEANUP_GROUP(h); /// Implements the operator !=. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator !=(PTP_CLEANUP_GROUP h1, PTP_CLEANUP_GROUP h2) => !(h1 == h2); /// Implements the operator ==. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator ==(PTP_CLEANUP_GROUP h1, PTP_CLEANUP_GROUP h2) => h1.Equals(h2); /// public override bool Equals(object obj) => obj is PTP_CLEANUP_GROUP h ? handle == h.handle : false; /// public override int GetHashCode() => handle.GetHashCode(); /// public IntPtr DangerousGetHandle() => handle; } /// Provides a handle to a threadpool IO. [StructLayout(LayoutKind.Sequential)] public struct PTP_IO : IHandle { private IntPtr handle; /// Initializes a new instance of the struct. /// An object that represents the pre-existing handle to use. public PTP_IO(IntPtr preexistingHandle) => handle = preexistingHandle; /// Returns an invalid handle by instantiating a object with . public static PTP_IO NULL => new PTP_IO(IntPtr.Zero); /// Gets a value indicating whether this instance is a null handle. public bool IsNull => handle == IntPtr.Zero; /// Performs an explicit conversion from to . /// The handle. /// The result of the conversion. public static explicit operator IntPtr(PTP_IO h) => h.handle; /// Performs an implicit conversion from to . /// The pointer to a handle. /// The result of the conversion. public static implicit operator PTP_IO(IntPtr h) => new PTP_IO(h); /// Implements the operator !=. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator !=(PTP_IO h1, PTP_IO h2) => !(h1 == h2); /// Implements the operator ==. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator ==(PTP_IO h1, PTP_IO h2) => h1.Equals(h2); /// public override bool Equals(object obj) => obj is PTP_IO h ? handle == h.handle : false; /// public override int GetHashCode() => handle.GetHashCode(); /// public IntPtr DangerousGetHandle() => handle; } /// Provides a handle to a thread pool. [StructLayout(LayoutKind.Sequential)] public struct PTP_POOL : IHandle { private IntPtr handle; /// Initializes a new instance of the struct. /// An object that represents the pre-existing handle to use. public PTP_POOL(IntPtr preexistingHandle) => handle = preexistingHandle; /// Returns an invalid handle by instantiating a object with . public static PTP_POOL NULL => new PTP_POOL(IntPtr.Zero); /// Gets a value indicating whether this instance is a null handle. public bool IsNull => handle == IntPtr.Zero; /// Performs an explicit conversion from to . /// The handle. /// The result of the conversion. public static explicit operator IntPtr(PTP_POOL h) => h.handle; /// Performs an implicit conversion from to . /// The pointer to a handle. /// The result of the conversion. public static implicit operator PTP_POOL(IntPtr h) => new PTP_POOL(h); /// Implements the operator !=. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator !=(PTP_POOL h1, PTP_POOL h2) => !(h1 == h2); /// Implements the operator ==. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator ==(PTP_POOL h1, PTP_POOL h2) => h1.Equals(h2); /// public override bool Equals(object obj) => obj is PTP_POOL h ? handle == h.handle : false; /// public override int GetHashCode() => handle.GetHashCode(); /// public IntPtr DangerousGetHandle() => handle; } /// Provides a handle to a threadpool timer. [StructLayout(LayoutKind.Sequential)] public struct PTP_TIMER : IHandle { private IntPtr handle; /// Initializes a new instance of the struct. /// An object that represents the pre-existing handle to use. public PTP_TIMER(IntPtr preexistingHandle) => handle = preexistingHandle; /// Returns an invalid handle by instantiating a object with . public static PTP_TIMER NULL => new PTP_TIMER(IntPtr.Zero); /// Gets a value indicating whether this instance is a null handle. public bool IsNull => handle == IntPtr.Zero; /// Performs an explicit conversion from to . /// The handle. /// The result of the conversion. public static explicit operator IntPtr(PTP_TIMER h) => h.handle; /// Performs an implicit conversion from to . /// The pointer to a handle. /// The result of the conversion. public static implicit operator PTP_TIMER(IntPtr h) => new PTP_TIMER(h); /// Implements the operator !=. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator !=(PTP_TIMER h1, PTP_TIMER h2) => !(h1 == h2); /// Implements the operator ==. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator ==(PTP_TIMER h1, PTP_TIMER h2) => h1.Equals(h2); /// public override bool Equals(object obj) => obj is PTP_TIMER h ? handle == h.handle : false; /// public override int GetHashCode() => handle.GetHashCode(); /// public IntPtr DangerousGetHandle() => handle; } /// Provides a handle to a threadpool wait. [StructLayout(LayoutKind.Sequential)] public struct PTP_WAIT : IHandle { private IntPtr handle; /// Initializes a new instance of the struct. /// An object that represents the pre-existing handle to use. public PTP_WAIT(IntPtr preexistingHandle) => handle = preexistingHandle; /// Returns an invalid handle by instantiating a object with . public static PTP_WAIT NULL => new PTP_WAIT(IntPtr.Zero); /// Gets a value indicating whether this instance is a null handle. public bool IsNull => handle == IntPtr.Zero; /// Performs an explicit conversion from to . /// The handle. /// The result of the conversion. public static explicit operator IntPtr(PTP_WAIT h) => h.handle; /// Performs an implicit conversion from to . /// The pointer to a handle. /// The result of the conversion. public static implicit operator PTP_WAIT(IntPtr h) => new PTP_WAIT(h); /// Implements the operator !=. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator !=(PTP_WAIT h1, PTP_WAIT h2) => !(h1 == h2); /// Implements the operator ==. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator ==(PTP_WAIT h1, PTP_WAIT h2) => h1.Equals(h2); /// public override bool Equals(object obj) => obj is PTP_WAIT h ? handle == h.handle : false; /// public override int GetHashCode() => handle.GetHashCode(); /// public IntPtr DangerousGetHandle() => handle; } /// Provides a handle to a threadpool work. [StructLayout(LayoutKind.Sequential)] public struct PTP_WORK : IHandle { private IntPtr handle; /// Initializes a new instance of the struct. /// An object that represents the pre-existing handle to use. public PTP_WORK(IntPtr preexistingHandle) => handle = preexistingHandle; /// Returns an invalid handle by instantiating a object with . public static PTP_WORK NULL => new PTP_WORK(IntPtr.Zero); /// Gets a value indicating whether this instance is a null handle. public bool IsNull => handle == IntPtr.Zero; /// Performs an explicit conversion from to . /// The handle. /// The result of the conversion. public static explicit operator IntPtr(PTP_WORK h) => h.handle; /// Performs an implicit conversion from to . /// The pointer to a handle. /// The result of the conversion. public static implicit operator PTP_WORK(IntPtr h) => new PTP_WORK(h); /// Implements the operator !=. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator !=(PTP_WORK h1, PTP_WORK h2) => !(h1 == h2); /// Implements the operator ==. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator ==(PTP_WORK h1, PTP_WORK h2) => h1.Equals(h2); /// public override bool Equals(object obj) => obj is PTP_WORK h ? handle == h.handle : false; /// public override int GetHashCode() => handle.GetHashCode(); /// public IntPtr DangerousGetHandle() => handle; } /// Used to set the stack reserve and commit sizes for new threads in a thread pool. [PInvokeData("threadpoolapiset.h")] [StructLayout(LayoutKind.Sequential)] public struct TP_POOL_STACK_INFORMATION { /// The stack reserve size. public SizeT StackReserve; /// The stack commit size. public SizeT StackCommit; } /// Defines a callback environment. /// ` [PInvokeData("threadpoolapiset.h")] [StructLayout(LayoutKind.Sequential)] public class PTP_CALLBACK_ENVIRON { internal uint Version; internal PTP_POOL Pool; internal PTP_CLEANUP_GROUP CleanupGroup; internal CleanupGroupCancelCallback CleanupGroupCancelCallback; internal HINSTANCE RaceDll; internal HACTCTX _ActivationContext; internal SimpleCallback _FinalizationCallback; internal TP_CALLBACK_ENV_FLAGS Flags; internal TP_CALLBACK_PRIORITY CallbackPriority; internal uint Size; internal PTP_CALLBACK_ENVIRON() { var isMin7 = Environment.OSVersion.Version >= new Version(6, 1); Version = isMin7 ? 3U : 1U; if (isMin7) { CallbackPriority = TP_CALLBACK_PRIORITY.TP_CALLBACK_PRIORITY_NORMAL; Size = (uint)Marshal.SizeOf(typeof(PTP_CALLBACK_ENVIRON)); } } /// Indicates a function to call when the callback environment is finalized. /// Pointer to a TP_SIMPLE_CALLBACK structure indicating a function to call when the callback environment is finalized. public SimpleCallback FinalizationCallback { set => _FinalizationCallback = value; } /// Assigns an activation context to the callback environment. /// Pointer to an _ACTIVATION_CONTEXT structure. public HACTCTX ActivationContext { set => _ActivationContext = value; } } /// Provides a for that is disposed using . public class SafePTP_CLEANUP_GROUP : SafeHANDLE { /// Initializes a new instance of the class and assigns an existing handle. /// An object that represents the pre-existing handle to use. /// /// to reliably release the handle during the finalization phase; otherwise, (not recommended). /// public SafePTP_CLEANUP_GROUP(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { } /// Initializes a new instance of the class. private SafePTP_CLEANUP_GROUP() : base() { } /// Gets or sets a value indicating whether to call CloseThreadpoolCleanupGroupMembers on disposal. public bool AutoCloseMembers { get; set; } /// Performs an implicit conversion from to . /// The safe handle instance. /// The result of the conversion. public static implicit operator PTP_CLEANUP_GROUP(SafePTP_CLEANUP_GROUP h) => h.handle; /// /// Releases the members of this cleanup group, waits for all callback functions to complete, and optionally cancels any /// outstanding callback functions. /// /// /// If this parameter is TRUE, the function cancels outstanding callbacks that have not yet started. If this parameter is FALSE, /// the function waits for outstanding callback functions to complete. /// /// /// The application-defined data to pass to the application's cleanup group callback function. You can specify the callback /// function when you call SetThreadpoolCallbackCleanupGroup. /// /// This function does not return a value. public void CloseMembers(bool fCancelPendingCallbacks, [Optional] IntPtr pvCleanupContext) => CloseThreadpoolCleanupGroupMembers(handle, fCancelPendingCallbacks, pvCleanupContext); /// Creates a new timer object. /// The callback function to call each time the timer object expires. For details, see TimerCallback. /// Optional application-defined data to pass to the callback function. /// /// /// A TP_CALLBACK_ENVIRON structure that defines the environment in which to execute the callback. The /// InitializeThreadpoolEnvironment function returns this structure. /// /// If this parameter is NULL, the callback executes in the default callback environment. For more information, see InitializeThreadpoolEnvironment. /// /// /// /// If the function succeeds, it returns a TP_TIMER structure that defines the timer object. Applications do not modify /// the members of this structure. /// /// If the function fails, it returns NULL. To retrieve extended error information, call GetLastError. /// public PTP_TIMER CreateTimer(TimerCallback pfnti, IntPtr pv = default, PTP_CALLBACK_ENVIRON pcbe = null) => InternalCreateThreadpoolTimer(pfnti, pv, pcbe); /// Creates a new wait object. /// The callback function to call when the wait completes or times out. For details, see WaitCallback. /// Optional application-defined data to pass to the callback function. /// /// /// A TP_CALLBACK_ENVIRON structure that defines the environment in which to execute the callback. The /// InitializeThreadpoolEnvironment function returns this structure. /// /// If this parameter is NULL, the callback executes in the default callback environment. For more information, see InitializeThreadpoolEnvironment. /// /// /// /// If the function succeeds, it returns a TP_WAIT structure that defines the wait object. Applications do not modify the /// members of this structure. /// /// If the function fails, it returns NULL. To retrieve extended error information, call GetLastError. /// public PTP_WAIT CreateWait(WaitCallback pfnwa, IntPtr pv = default, PTP_CALLBACK_ENVIRON pcbe = null) => InternalCreateThreadpoolWait(pfnwa, pv, pcbe); /// Creates a new work object. /// /// The callback function. A worker thread calls this callback each time you call SubmitThreadpoolWork to post the work /// object. For details, see WorkCallback. /// /// Optional application-defined data to pass to the callback function. /// /// /// A TP_CALLBACK_ENVIRON structure that defines the environment in which to execute the callback. The /// InitializeThreadpoolEnvironment function returns this structure. /// /// If this parameter is NULL, the callback executes in the default callback environment. For more information, see InitializeThreadpoolEnvironment. /// /// /// /// If the function succeeds, it returns a TP_WORK structure that defines the work object. Applications do not modify the /// members of this structure. /// /// If the function fails, it returns NULL. To retrieve extended error information, call GetLastError. /// public PTP_WORK CreateWork(WorkCallback pfnwk, IntPtr pv = default, PTP_CALLBACK_ENVIRON pcbe = null) => InternalCreateThreadpoolWork(pfnwk, pv, pcbe); /// protected override bool InternalReleaseHandle() { if (AutoCloseMembers) CloseMembers(false); CloseThreadpoolCleanupGroup(handle); return true; } } /// Provides a for that is disposed using . public class SafePTP_IO : SafeHANDLE { /// Initializes a new instance of the class and assigns an existing handle. /// An object that represents the pre-existing handle to use. /// /// to reliably release the handle during the finalization phase; otherwise, (not recommended). /// public SafePTP_IO(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { } /// Initializes a new instance of the class. private SafePTP_IO() : base() { } /// Performs an implicit conversion from to . /// The safe handle instance. /// The result of the conversion. public static implicit operator PTP_IO(SafePTP_IO h) => h.handle; /// protected override bool InternalReleaseHandle() { CloseThreadpoolIo(handle); return true; } } /// Provides a for that is disposed using . public class SafePTP_POOL : SafeHANDLE { /// Initializes a new instance of the class and assigns an existing handle. /// An object that represents the pre-existing handle to use. /// /// to reliably release the handle during the finalization phase; otherwise, (not recommended). /// public SafePTP_POOL(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { } /// Initializes a new instance of the class. private SafePTP_POOL() : base() { } /// Performs an implicit conversion from to . /// The safe handle instance. /// The result of the conversion. public static implicit operator PTP_POOL(SafePTP_POOL h) => h.handle; /// protected override bool InternalReleaseHandle() { CloseThreadpool(handle); return true; } } /// Provides a for that is disposed using . public class SafePTP_TIMER : SafeHANDLE { /// Initializes a new instance of the class and assigns an existing handle. /// An object that represents the pre-existing handle to use. /// /// to reliably release the handle during the finalization phase; otherwise, (not recommended). /// public SafePTP_TIMER(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { } /// Initializes a new instance of the class. private SafePTP_TIMER() : base() { } /// Performs an implicit conversion from to . /// The safe handle instance. /// The result of the conversion. public static implicit operator PTP_TIMER(SafePTP_TIMER h) => h.handle; /// protected override bool InternalReleaseHandle() { CloseThreadpoolTimer(handle); return true; } } /// Provides a for that is disposed using . public class SafePTP_WAIT : SafeHANDLE { /// Initializes a new instance of the class and assigns an existing handle. /// An object that represents the pre-existing handle to use. /// /// to reliably release the handle during the finalization phase; otherwise, (not recommended). /// public SafePTP_WAIT(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { } /// Initializes a new instance of the class. private SafePTP_WAIT() : base() { } /// Performs an implicit conversion from to . /// The safe handle instance. /// The result of the conversion. public static implicit operator PTP_WAIT(SafePTP_WAIT h) => h.handle; /// protected override bool InternalReleaseHandle() { CloseThreadpoolWait(handle); return true; } } /// Provides a for that is disposed using . public class SafePTP_WORK : SafeHANDLE { /// Initializes a new instance of the class and assigns an existing handle. /// An object that represents the pre-existing handle to use. /// /// to reliably release the handle during the finalization phase; otherwise, (not recommended). /// public SafePTP_WORK(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { } /// Initializes a new instance of the class. private SafePTP_WORK() : base() { } /// Performs an implicit conversion from to . /// The safe handle instance. /// The result of the conversion. public static implicit operator PTP_WORK(SafePTP_WORK h) => h.handle; /// protected override bool InternalReleaseHandle() { CloseThreadpoolWork(handle); return true; } } } }