using System; using System.Runtime.InteropServices; namespace Vanara.PInvoke { public static partial class Shell32 { /// Accept the default priority assigned to the task by the scheduler. public const uint ITSAT_DEFAULT_PRIORITY = 0x10000000; /// High priority. public const uint ITSAT_MAX_PRIORITY = 0x7fffffff; /// Low priority. public const uint ITSAT_MIN_PRIORITY = 0x00000000; /// Default milliseconds until a sleeping worker thread is released public const uint ITSS_THREAD_DESTROY_DEFAULT_TIMEOUT = 10 * 1000; /// Set sleeping worker threads to never be released. public const uint ITSS_THREAD_TERMINATE_TIMEOUT = Kernel32.INFINITE; /// No change to the thread timeout public const uint ITSS_THREAD_TIMEOUT_NO_CHANGE = Kernel32.INFINITE - 1; /// Default value for lParam parameter. public static readonly IntPtr ITSAT_DEFAULT_LPARAM = (IntPtr)(-1); /// Indicates the current execution state. [PInvokeData("shobjidl_core.h", MSDNShortId = "NN:shobjidl_core.IRunnableTask")] public enum IRTIR_TASK { /// Extraction has not yet started. IRTIR_TASK_NOT_RUNNING = 0, /// The task is running. IRTIR_TASK_RUNNING = 1, /// The task is suspended. IRTIR_TASK_SUSPENDED = 2, /// IRunnableTask::Kill has been called on the thread, but the thread has not yet completely shut down. IRTIR_TASK_PENDING = 3, /// The task is finished. IRTIR_TASK_FINISHED = 4 } /// The release status for . [PInvokeData("shobjidl_core.h", MSDNShortId = "NN:shobjidl_core.IShellTaskScheduler")] [Flags] public enum ITSSFLAG : uint { /// Wait for the current task to complete before deleting the scheduler. ITSSFLAG_COMPLETE_ON_DESTROY = 0x0000, /// Immediately cease execution of the current task when the IShellTaskScheduler instance is released. ITSSFLAG_KILL_ON_DESTROY = 0x0001 } /// /// A free-threaded interface that can be exposed by an object to allow operations to be performed on a background thread. For /// example, if the IExtractImage::GetLocation method returns E_PENDING, the calling application is permitted to extract the image /// on a background thread. /// /// /// /// Implement IRunnableTask if your namespace extension is free-threaded, and you want to allow a task such as icon /// extraction to be managed by a scheduler. Only the Run and IsRunning methods must be implemented. If you do not want to implement /// Kill, Resume, and Suspend, simply have them return E_NOTIMPL. /// /// /// If you are using IRunnableTask to extract an image on a background thread, that is, if the object exposes IExtractImage, /// then Run is not necessary, as the system will use IExtractImage::Extract to manage the task. The other methods (Kill, Resume, /// and Suspend) are optional in this case, but will be used by the system if they are implemented. /// /// /// You do not call this interface directly. IRunnableTask is used by the operating system only when it has confirmed that /// your application is aware of this interface. /// /// IRunnableTask implements IUnknown as well as the five listed methods. /// NoteWindows Vista and later. Prior to Windows Vista this interface was declared in Shlobj.h. /// // https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nn-shobjidl_core-irunnabletask [PInvokeData("shobjidl_core.h", MSDNShortId = "NN:shobjidl_core.IRunnableTask")] [ComImport, Guid("85788d00-6807-11d0-b810-00c04fd706ec"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface IRunnableTask { /// Requests that a task begin. /// /// Type: HRESULT /// Returns one of the following two codes. /// /// /// Return code /// Description /// /// /// S_OK /// Execution is complete. /// /// /// E_PENDING /// Execution is suspended. /// /// /// /// /// The return value of this method only tells you whether the execution of the task completed or is suspended. Any other errors /// that the implementer needs to communicate to the caller must be provided through other channels, such as a callback function. /// // https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-irunnabletask-run HRESULT Run(); [PreserveSig] HRESULT Run(); /// Requests that a task be stopped. /// /// Type: BOOL /// Not currently used. /// /// /// Type: HRESULT /// If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. /// /// /// Implementation of this method is optional. If you do not wish to support this functionality, create a token implementation /// that simply returns E_NOTIMPL. /// // https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-irunnabletask-kill HRESULT Kill( BOOL bWait ); [PreserveSig] HRESULT Kill([MarshalAs(UnmanagedType.Bool)] bool bWait); /// Requests that a task be suspended. /// /// Type: HRESULT /// Return S_OK if successful, or standard COM-defined error codes otherwise. /// /// /// Implementation of this method is optional. If you do not wish to support this functionality, create a token implementation /// that simply returns E_NOTIMPL. /// // https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-irunnabletask-suspend HRESULT Suspend(); [PreserveSig] HRESULT Suspend(); /// Requests that a task resume. /// /// Type: HRESULT /// Returns S_OK if successful, or standard COM-defined error codes otherwise. /// /// /// Implementation of this method is optional. If you do not wish to support this functionality, create a token implementation /// that simply returns E_NOTIMPL. /// // https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-irunnabletask-resume HRESULT Resume(); [PreserveSig] HRESULT Resume(); /// Requests information on the state of a task, such as thumbnail extraction. /// /// Type: LONG /// Returns one of the following values to indicate the current execution state. /// /// /// Return code /// Description /// /// /// IRTIR_TASK_NOT_RUNNING /// Extraction has not yet started. /// /// /// IRTIR_TASK_RUNNING /// The task is running. /// /// /// IRTIR_TASK_SUSPENDED /// The task is suspended. /// /// /// IRTIR_TASK_PENDING /// IRunnableTask::Kill has been called on the thread, but the thread has not yet completely shut down. /// /// /// IRTIR_TASK_FINISHED /// The task is finished. /// /// /// /// This method must be implemented. // https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-irunnabletask-isrunning ULONG IsRunning(); [PreserveSig] IRTIR_TASK IsRunning(); } /// /// /// [ IShellTaskScheduler is available for use in the operating systems specified in the Requirements section. It may be /// altered or unavailable in subsequent versions.] /// /// Exposes methods that enable interaction with, and control of, a task scheduler. /// /// /// /// This interface does not need to be free-threaded unless the items in the queue interact with the scheduler as well as the main /// execution thread on which the task scheduler was created. /// /// This interface's class identifier (CLSID) is CLSID_ShellTaskScheduler, and its IID is IID_IShellTaskScheduler. /// Windows Server 2003 and Windows XP:IShellTaskScheduler was declared in Shlobj.h. /// // https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nn-shobjidl_core-ishelltaskscheduler [PInvokeData("shobjidl_core.h", MSDNShortId = "NN:shobjidl_core.IShellTaskScheduler")] [ComImport, Guid("6CCB7BE0-6807-11d0-B810-00C04FD706EC"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface IShellTaskScheduler { /// Adds a task to the scheduler's background queue. /// /// Type: IRunnableTask* /// A pointer to an instance of an IRunnableTask interface representing the task to add to the queue. /// /// /// Type: REFTASKOWNERID /// /// A GUID identifying the owner of the task. This information can be used to group tasks for later counting or removal by owner. /// /// /// /// Type: DWORD_PTR /// /// A pointer to a user-defined DWORD value allowing the task to be identified within the tasks owned by rtoid. This is /// used to identify single tasks or to subgroup them, for instance associating the task with a particular item such as an item /// in a ListView. This parameter can be zero. /// /// /// /// Type: DWORD /// /// One of the following values assigning the task's priority. Response to this priority depends on the cooperation of the other /// tasks being executed. New tasks are inserted in the queue in priority order. If a task of a low priority is currently under /// execution when a higher priority task is added, the scheduler attempts to suspend the task under execution. That lower /// priority task is resumed when the higher priority task(s) are completed. /// /// ITSAT_DEFAULT_PRIORITY /// Accept the default priority assigned to the task by the scheduler. /// ITSAT_MAX_PRIORITY /// High priority. /// ITSAT_MIN_PRIORITY /// Low priority. /// // https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ishelltaskscheduler-addtask HRESULT // AddTask( IRunnableTask *prt, REFTASKOWNERID rtoid, DWORD_PTR lParam, DWORD dwPriority ); void AddTask([In] IRunnableTask prt, in Guid rtoid, [In] IntPtr lParam, [In] uint dwPriority = ITSAT_DEFAULT_PRIORITY); /// Removes tasks from the scheduler's background queue. /// /// Type: REFTASKOWNERID /// A GUID identifying the owner of the tasks to remove. /// /// /// Type: DWORD_PTR /// /// A pointer to a user-defined DWORD value that allows the task to be identified within the tasks owned by rtoid. Set /// this value to 0 to remove all tasks for the owner specified by rtoid. /// /// /// /// Type: BOOL /// TRUE if you want a currently running task to complete before removing it, FALSE otherwise. /// // https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ishelltaskscheduler-removetasks HRESULT // RemoveTasks( REFTASKOWNERID rtoid, DWORD_PTR lParam, BOOL bWaitIfRunning ); void RemoveTasks(in Guid rtoid, [In] IntPtr lParam, [MarshalAs(UnmanagedType.Bool)] bool bWaitIfRunning); /// Counts tasks with the same owner ID in the scheduler's queue. /// /// Type: REFTASKOWNERID /// /// A GUID identifying the owner of the tasks. Supplying a specific ID will count only those tasks tagged with that owner ID. To /// count all items in the queue, pass TOID_NULL. /// /// /// /// Type: HRESULT /// If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code. /// // https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ishelltaskscheduler-counttasks UINT // CountTasks( REFTASKOWNERID rtoid ); uint CountTasks(in Guid rtoid); /// Sets the release status and background thread timeout for the current task. /// /// Type: DWORD /// The following flag or 0. /// ITSSFLAG_KILL_ON_DESTROY /// Immediately cease execution of the current task when the IShellTaskScheduler instance is released. /// /// /// Type: DWORD /// Not used. /// // https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ishelltaskscheduler-status HRESULT Status( // DWORD dwReleaseStatus, DWORD dwThreadTimeout ); void Status([In] uint dwReleaseStatus, [In, Optional] uint dwThreadTimeout); } } }