|
|||||
<このサンプルの概要> タスク スケジュールを登録するサンプルです。定期的に実行(起動)するタスク登録に如何でしょうか。 3流プログラマのメモ書き様(http://jehupc.exblog.jp/)のサンプルを参考にしています。 こちらでは、タスク登録方法を色々な方法を試しています。試している事は以下の項目です。 1.一回だけ実行するタスク・スケジューラの作成 2.毎日(または2日に)、1回だけ実行するタスク・スケジューラの作成 3.毎日、3時間に1回、実行するタスク・スケジューラの作成 4.毎週、月水金曜日に実行するタスク・スケジューラの作成 5.毎月、15日に実行するタスク・スケジューラの作成 実は、タスク スケジューラを登録するサンプルは「古いごみ削除ツール(NonRccDel)」で 使用したいと思い勉強したのですが、まだ実装出来ていません。 代わりにマニュアルを作りましたが... クラス(Class1.vb) Imports System.Runtime.InteropServices Imports System.Runtime.InteropServices.ComTypes Public Class Class1 Private Shared CLSID_CTaskScheduler As New Guid("{148BD52A-A2AB-11CE-B11F-00AA00530503}") Private Shared CLSID_CTask As New Guid("{148BD520-A2AB-11CE-B11F-00AA00530503}") Private Shared IID_ITaskScheduler As New Guid("{148BD527-A2AB-11CE-B11F-00AA00530503}") Private Shared IID_ITask As New Guid("{148BD524-A2AB-11CE-B11F-00AA00530503}") Private Enum CLSCTX As Integer CLSCTX_INPROC_SERVER = &H1 CLSCTX_INPROC_HANDLER = &H2 CLSCTX_LOCAL_SERVER = &H4 CLSCTX_INPROC_SERVER16 = &H8 CLSCTX_REMOTE_SERVER = &H10 CLSCTX_INPROC_HANDLER16 = &H20 CLSCTX_RESERVED1 = &H40 CLSCTX_RESERVED2 = &H80 CLSCTX_RESERVED3 = &H100 CLSCTX_RESERVED4 = &H200 CLSCTX_NO_CODE_DOWNLOAD = &H400 CLSCTX_RESERVED5 = &H800 CLSCTX_NO_CUSTOM_MARSHAL = &H1000 CLSCTX_ENABLE_CODE_DOWNLOAD = &H2000 CLSCTX_NO_FAILURE_LOG = &H4000 CLSCTX_DISABLE_AAA = &H8000 CLSCTX_ENABLE_AAA = &H10000 CLSCTX_FROM_DEFAULT_CONTEXT = &H20000 CLSCTX_INPROC = CLSCTX_INPROC_SERVER Or CLSCTX_INPROC_HANDLER CLSCTX_SERVER = CLSCTX_INPROC_SERVER Or CLSCTX_LOCAL_SERVER Or CLSCTX_REMOTE_SERVER CLSCTX_ALL = CLSCTX_SERVER Or CLSCTX_INPROC_HANDLER End Enum Private Enum TASK_TRIGGER_TYPE As Integer TASK_TIME_TRIGGER_ONCE = 0 TASK_TIME_TRIGGER_DAILY = 1 TASK_TIME_TRIGGER_WEEKLY = 2 TASK_TIME_TRIGGER_MONTHLYDATE = 3 TASK_TIME_TRIGGER_MONTHLYDOW = 4 TASK_EVENT_TRIGGER_ON_IDLE = 5 TASK_EVENT_TRIGGER_AT_SYSTEMSTART = 6 TASK_EVENT_TRIGGER_AT_LOGON = 7 End Enum Private Structure DAILY Public DaysInterval As Short End Structure Private Structure WEEKLY Public WeeksInterval As Short Public rgfDaysOfTheWeek As Short End Structure Private Structure MONTHLYDATE Public rgfDays As Integer Public rgfMonths As Short End Structure Private Structure MONTHLYDOW Public wWhichWeek As Short Public rgfDaysOfTheWeek As Short Public rgfMonths As Short End Structure <StructLayout(LayoutKind.Explicit)> _ Private Structure TRIGGER_TYPE_UNION <FieldOffset(0)> Public Daily As DAILY <FieldOffset(0)> Public Weekly As WEEKLY <FieldOffset(0)> Public MonthlyDate As MONTHLYDATE <FieldOffset(0)> Public MonthlyDOW As MONTHLYDOW End Structure <StructLayout(LayoutKind.Sequential)> _ Private Structure TASK_TRIGGER Public cbTriggerSize As Short Public Reserved1 As Short Public wBeginYear As Short Public wBeginMonth As Short Public wBeginDay As Short Public wEndYear As Short Public wEndMonth As Short Public wEndDay As Short Public wStartHour As Short Public wStartMinute As Short Public MinutesDuration As Integer Public MinutesInterval As Integer Public rgFlags As Integer Public TriggerType As TASK_TRIGGER_TYPE Public Type As TRIGGER_TYPE_UNION Public Reserved2 As Short Public wRandomMinutesInterval As Short End Structure <InterfaceType(ComInterfaceType.InterfaceIsIUnknown), _ Guid("148BD527-A2AB-11CE-B11F-00AA00530503")> _ Private Interface ITaskScheduler Sub SetTargetComputer() Sub GetTargetComputer() Sub Enum_() Sub Activate() Sub Delete() Function NewWorkItem( _ <[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal pwszTaskName As String, _ <[In](), MarshalAs(UnmanagedType.LPStruct)> ByVal rclsid As Guid, _ <[In](), MarshalAs(UnmanagedType.LPStruct)> ByVal riid As Guid _ ) As <MarshalAs(UnmanagedType.Interface)> Object Sub AddWorkItem() Sub IsOfType() End Interface <InterfaceType(ComInterfaceType.InterfaceIsIUnknown), _ Guid("148BD524-A2AB-11CE-B11F-00AA00530503")> _ Private Interface ITask Function CreateTrigger(ByRef iNewTrigger As Short _ ) As <MarshalAs(UnmanagedType.Interface)> Object Sub DeleteTrigger() Sub GetTriggerCount() Sub GetTrigger() Sub GetTriggerString() Sub GetRunTimes() Sub GetNextRunTime() Sub SetIdleWait() Sub GetIdleWait() Sub Run() Sub Terminate() Sub EditWorkItem() Sub GetMostRecentRunTime() Sub GetStatus() Sub GetExitCode() Sub SetComment() Sub GetComment() Sub SetCreator() Sub GetCreator() Sub SetWorkItemData() Sub GetWorkItemData() Sub SetErrorRetryCount() Sub GetErrorRetryCount() Sub SetErrorRetryInterval() Sub GetErrorRetryInterval() Sub SetFlags() Sub GetFlags() Sub SetAccountInformation( _ <[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal pwszAccountName As String, _ <[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal pwszPassword As String) Sub GetAccountInformation() Sub SetApplicationName( _ <[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal pwszApplicationName As String) Sub GetApplicationName() Sub SetParameters() Sub GetParameters() Sub SetWorkingDirectory() Sub GetWorkingDirectory() Sub SetPriority() Sub GetPriority() Sub SetTaskFlags() Sub GetTaskFlags() Sub SetMaxRunTime() Sub GetMaxRunTime() End Interface <InterfaceType(ComInterfaceType.InterfaceIsIUnknown), _ Guid("148BD52B-A2AB-11CE-B11F-00AA00530503")> _ Private Interface ITaskTrigger Sub SetTrigger(ByRef pTrigger As TASK_TRIGGER) Sub GetTrigger() Sub GetTriggerString() End Interface <DllImport("ole32.dll", ExactSpelling:=True, PreserveSig:=False)> _ Private Shared Function CoCreateInstance( _ <[In](), MarshalAs(UnmanagedType.LPStruct)> ByVal rclsid As Guid, _ <MarshalAs(UnmanagedType.IUnknown)> ByVal pUnkOuter As Object, _ ByVal dwClsContext As CLSCTX, _ <[In](), MarshalAs(UnmanagedType.LPStruct)> ByVal riid As Guid _ ) As <MarshalAs(UnmanagedType.Interface)> Object End Function Private objTaskScheduler As ITaskScheduler = Nothing Private objTask As ITask = Nothing Private objTaskTrigger As ITaskTrigger = Nothing Private objPersistFile As IPersistFile = Nothing ' 1回だけ実行するタスク・スケジューラの作成 Public Sub Test1() Try ' タスク作成(タスク名) objTaskScheduler = CoCreateInstance( _ CLSID_CTaskScheduler, Nothing, CLSCTX.CLSCTX_INPROC_SERVER, IID_ITaskScheduler) objTask = objTaskScheduler.NewWorkItem("TaskName1", CLSID_CTask, IID_ITask) ' 実行プログラム設定 objTask.SetApplicationName("notepad.exe") ' 実行アカウント名とパスワード設定 objTask.SetAccountInformation("user", "password") ' トリガー作成 Dim iNewTrigger As Short objTaskTrigger = CType(objTask.CreateTrigger(iNewTrigger), ITaskTrigger) ' スケジュール設定------------------------------------------------------ Dim pTrigger As New TASK_TRIGGER() pTrigger.cbTriggerSize = CType(Marshal.SizeOf(pTrigger), Short) pTrigger.wBeginYear = 2100 ' 2100年 pTrigger.wBeginMonth = 12 ' 12月 pTrigger.wBeginDay = 31 ' 31日 pTrigger.wStartHour = 17 ' 17時 pTrigger.wStartMinute = 30 ' 30分 pTrigger.TriggerType = TASK_TRIGGER_TYPE.TASK_TIME_TRIGGER_ONCE objTaskTrigger.SetTrigger(pTrigger) ' ---------------------------------------------------------------------- ' タスク保存 Dim pPersistFile As IntPtr = IntPtr.Zero Dim IID_IPersistFile As Guid = Marshal.GenerateGuidForType(GetType(IPersistFile)) Marshal.QueryInterface( _ Marshal.GetIUnknownForObject(objTask), IID_IPersistFile, pPersistFile) objPersistFile = CType(Marshal.GetObjectForIUnknown(pPersistFile), IPersistFile) objPersistFile.Save(Nothing, True) Catch ex As Exception Throw ex Finally ' COMオブジェクト解放 ReleaseComObject() End Try End Sub ' 毎日(または2日に)、1回だけ実行するタスク・スケジューラの作成 Public Sub Test2() Try ' タスク作成(タスク名) objTaskScheduler = CoCreateInstance( _ CLSID_CTaskScheduler, Nothing, CLSCTX.CLSCTX_INPROC_SERVER, IID_ITaskScheduler) objTask = objTaskScheduler.NewWorkItem("TaskName2", CLSID_CTask, IID_ITask) ' 実行プログラム設定 objTask.SetApplicationName("notepad.exe") ' 実行アカウント名とパスワード設定 objTask.SetAccountInformation("user", "password") ' トリガー作成 Dim iNewTrigger As Short objTaskTrigger = CType(objTask.CreateTrigger(iNewTrigger), ITaskTrigger) ' スケジュール設定------------------------------------------------------ Dim pTrigger As New TASK_TRIGGER() pTrigger.cbTriggerSize = CType(Marshal.SizeOf(pTrigger), Short) pTrigger.wBeginYear = 2100 ' 2100年 pTrigger.wBeginMonth = 12 ' 12月 pTrigger.wBeginDay = 31 ' 31日 pTrigger.wStartHour = 17 ' 17時 pTrigger.wStartMinute = 30 ' 30分 'pTrigger.Type.Daily.DaysInterval = 1 ' 1日に1回 pTrigger.Type.Daily.DaysInterval = 2 ' 2日に1回 pTrigger.TriggerType = TASK_TRIGGER_TYPE.TASK_TIME_TRIGGER_DAILY objTaskTrigger.SetTrigger(pTrigger) ' ---------------------------------------------------------------------- ' タスク保存 Dim pPersistFile As IntPtr = IntPtr.Zero Dim IID_IPersistFile As Guid = Marshal.GenerateGuidForType(GetType(IPersistFile)) Marshal.QueryInterface( _ Marshal.GetIUnknownForObject(objTask), IID_IPersistFile, pPersistFile) objPersistFile = CType(Marshal.GetObjectForIUnknown(pPersistFile), IPersistFile) objPersistFile.Save(Nothing, True) Catch ex As Exception Throw ex Finally ' COMオブジェクト解放 ReleaseComObject() End Try End Sub ' 毎日、3時間に1回、実行するタスク・スケジューラの作成 Public Sub Test3() Try ' タスク作成(タスク名) objTaskScheduler = CoCreateInstance( _ CLSID_CTaskScheduler, Nothing, CLSCTX.CLSCTX_INPROC_SERVER, IID_ITaskScheduler) objTask = objTaskScheduler.NewWorkItem("TaskName3", CLSID_CTask, IID_ITask) ' 実行プログラム設定 objTask.SetApplicationName("notepad.exe") ' 実行アカウント名とパスワード設定 objTask.SetAccountInformation("user", "password") ' トリガー作成 Dim iNewTrigger As Short objTaskTrigger = CType(objTask.CreateTrigger(iNewTrigger), ITaskTrigger) ' スケジュール設定------------------------------------------------------ Dim pTrigger As New TASK_TRIGGER() pTrigger.cbTriggerSize = CType(Marshal.SizeOf(pTrigger), Short) pTrigger.wBeginYear = 2100 ' 2100年 pTrigger.wBeginMonth = 12 ' 12月 pTrigger.wBeginDay = 31 ' 31日 pTrigger.wStartHour = 17 ' 17時 pTrigger.wStartMinute = 30 ' 30分 pTrigger.MinutesDuration = 24 * 60 ' 24時間継続 pTrigger.MinutesInterval = 3 * 60 ' 03時間間隔 pTrigger.Type.Daily.DaysInterval = 1 ' 1日に1回 pTrigger.TriggerType = TASK_TRIGGER_TYPE.TASK_TIME_TRIGGER_DAILY objTaskTrigger.SetTrigger(pTrigger) ' ---------------------------------------------------------------------- ' タスク保存 Dim pPersistFile As IntPtr = IntPtr.Zero Dim IID_IPersistFile As Guid = Marshal.GenerateGuidForType(GetType(IPersistFile)) Marshal.QueryInterface( _ Marshal.GetIUnknownForObject(objTask), IID_IPersistFile, pPersistFile) objPersistFile = CType(Marshal.GetObjectForIUnknown(pPersistFile), IPersistFile) objPersistFile.Save(Nothing, True) Catch ex As Exception Throw ex Finally ' COMオブジェクト解放 ReleaseComObject() End Try End Sub ' 毎週、月水金曜日に実行するタスク・スケジューラの作成 Public Sub Test4() Try ' タスク作成(タスク名) objTaskScheduler = CoCreateInstance( _ CLSID_CTaskScheduler, Nothing, CLSCTX.CLSCTX_INPROC_SERVER, IID_ITaskScheduler) objTask = objTaskScheduler.NewWorkItem("TaskName4", CLSID_CTask, IID_ITask) ' 実行プログラム設定 objTask.SetApplicationName("notepad.exe") ' 実行アカウント名とパスワード設定 objTask.SetAccountInformation("user", "password") ' トリガー作成 Dim iNewTrigger As Short objTaskTrigger = CType(objTask.CreateTrigger(iNewTrigger), ITaskTrigger) ' スケジュール設定------------------------------------------------------ Dim pTrigger As New TASK_TRIGGER() pTrigger.cbTriggerSize = CType(Marshal.SizeOf(pTrigger), Short) pTrigger.wBeginYear = 2100 ' 2100年 pTrigger.wBeginMonth = 12 ' 12月 pTrigger.wBeginDay = 31 ' 31日 pTrigger.wStartHour = 17 ' 17時 pTrigger.wStartMinute = 30 ' 30分 pTrigger.Type.Weekly.WeeksInterval = 1 ' 1週に1回 pTrigger.Type.Weekly.rgfDaysOfTheWeek = 2 + 8 + 32 ' 月水金曜日 pTrigger.TriggerType = TASK_TRIGGER_TYPE.TASK_TIME_TRIGGER_WEEKLY objTaskTrigger.SetTrigger(pTrigger) ' ---------------------------------------------------------------------- ' タスク保存 Dim pPersistFile As IntPtr = IntPtr.Zero Dim IID_IPersistFile As Guid = Marshal.GenerateGuidForType(GetType(IPersistFile)) Marshal.QueryInterface( _ Marshal.GetIUnknownForObject(objTask), IID_IPersistFile, pPersistFile) objPersistFile = CType(Marshal.GetObjectForIUnknown(pPersistFile), IPersistFile) objPersistFile.Save(Nothing, True) Catch ex As Exception Throw ex Finally ' COMオブジェクト解放 ReleaseComObject() End Try End Sub ' 毎月、15日に実行するタスク・スケジューラの作成 Public Sub Test5() Try ' タスク作成(タスク名) objTaskScheduler = CoCreateInstance( _ CLSID_CTaskScheduler, Nothing, CLSCTX.CLSCTX_INPROC_SERVER, IID_ITaskScheduler) objTask = objTaskScheduler.NewWorkItem("TaskName5", CLSID_CTask, IID_ITask) ' 実行プログラム設定 objTask.SetApplicationName("notepad.exe") ' 実行アカウント名とパスワード設定 objTask.SetAccountInformation("user", "password") ' トリガー作成 Dim iNewTrigger As Short objTaskTrigger = CType(objTask.CreateTrigger(iNewTrigger), ITaskTrigger) ' スケジュール設定------------------------------------------------------ Dim pTrigger As New TASK_TRIGGER() pTrigger.cbTriggerSize = CType(Marshal.SizeOf(pTrigger), Short) pTrigger.wBeginYear = 2100 ' 2100年 pTrigger.wBeginMonth = 12 ' 12月 pTrigger.wBeginDay = 31 ' 31日 pTrigger.wStartHour = 17 ' 17時 pTrigger.wStartMinute = 30 ' 30分 pTrigger.Type.MonthlyDate.rgfMonths = _ 1 Or 2 Or 4 Or 8 Or 16 Or 32 Or _ 64 Or 128 Or 256 Or 512 Or 1024 Or 2048 ' 1,2,3,4,5,6,7,8,9,10,11,12月 pTrigger.Type.MonthlyDate.rgfDays = 2 ^ (15 - 1) ' 15日 pTrigger.TriggerType = TASK_TRIGGER_TYPE.TASK_TIME_TRIGGER_MONTHLYDATE objTaskTrigger.SetTrigger(pTrigger) ' ---------------------------------------------------------------------- ' タスク保存 Dim pPersistFile As IntPtr = IntPtr.Zero Dim IID_IPersistFile As Guid = Marshal.GenerateGuidForType(GetType(IPersistFile)) Marshal.QueryInterface( _ Marshal.GetIUnknownForObject(objTask), IID_IPersistFile, pPersistFile) objPersistFile = CType(Marshal.GetObjectForIUnknown(pPersistFile), IPersistFile) objPersistFile.Save(Nothing, True) Catch ex As Exception Throw ex Finally ' COMオブジェクト解放 ReleaseComObject() End Try End Sub ' 毎月、第2木曜日に実行するタスク・スケジューラの作成 Public Sub Test6() Try ' タスク作成(タスク名) objTaskScheduler = CoCreateInstance( _ CLSID_CTaskScheduler, Nothing, CLSCTX.CLSCTX_INPROC_SERVER, IID_ITaskScheduler) objTask = objTaskScheduler.NewWorkItem("TaskName6", CLSID_CTask, IID_ITask) ' 実行プログラム設定 objTask.SetApplicationName("notepad.exe") ' 実行アカウント名とパスワード設定 objTask.SetAccountInformation("user", "password") ' トリガー作成 Dim iNewTrigger As Short objTaskTrigger = CType(objTask.CreateTrigger(iNewTrigger), ITaskTrigger) ' スケジュール設定------------------------------------------------------ Dim pTrigger As New TASK_TRIGGER() pTrigger.cbTriggerSize = CType(Marshal.SizeOf(pTrigger), Short) pTrigger.wBeginYear = 2100 ' 2100年 pTrigger.wBeginMonth = 12 ' 12月 pTrigger.wBeginDay = 31 ' 31日 pTrigger.wStartHour = 17 ' 17時 pTrigger.wStartMinute = 30 ' 30分 pTrigger.Type.MonthlyDOW.rgfMonths = _ 1 Or 2 Or 4 Or 8 Or 16 Or 32 Or _ 64 Or 128 Or 256 Or 512 Or 1024 Or 2048 ' 1,2,3,4,5,6,7,8,9,10,11,12月 pTrigger.Type.MonthlyDOW.wWhichWeek = 2 ' 第2週 pTrigger.Type.MonthlyDOW.rgfDaysOfTheWeek = 16 ' 木曜日 pTrigger.TriggerType = TASK_TRIGGER_TYPE.TASK_TIME_TRIGGER_MONTHLYDOW objTaskTrigger.SetTrigger(pTrigger) ' ---------------------------------------------------------------------- ' タスク保存 Dim pPersistFile As IntPtr = IntPtr.Zero Dim IID_IPersistFile As Guid = Marshal.GenerateGuidForType(GetType(IPersistFile)) Marshal.QueryInterface( _ Marshal.GetIUnknownForObject(objTask), IID_IPersistFile, pPersistFile) objPersistFile = CType(Marshal.GetObjectForIUnknown(pPersistFile), IPersistFile) objPersistFile.Save(Nothing, True) Catch ex As Exception Throw ex Finally ' COMオブジェクト解放 ReleaseComObject() End Try End Sub ''' <summary> ''' COMオブジェクト解放 ''' </summary> ''' <remarks></remarks> Private Sub ReleaseComObject() Try If objPersistFile Is Nothing = False Then Marshal.ReleaseComObject(objPersistFile) objPersistFile = Nothing End If Catch ex As Exception End Try Try If objTaskTrigger Is Nothing = False Then Marshal.ReleaseComObject(objTaskTrigger) objTaskTrigger = Nothing End If Catch ex As Exception End Try Try If objTask Is Nothing = False Then Marshal.ReleaseComObject(objTask) objTask = Nothing End If Catch ex As Exception End Try Try If objTaskScheduler Is Nothing = False Then Marshal.ReleaseComObject(objTaskScheduler) objTaskScheduler = Nothing End If Catch ex As Exception End Try End Sub ''' <summary> ''' デストラクタ ''' </summary> ''' <remarks></remarks> Protected Overrides Sub Finalize() MyBase.Finalize() ' COMオブジェクト解放 ReleaseComObject() End Sub End Class ★テスト用フォーム(Form1.vb) Public Class Form1 ' 1回だけ実行するタスク・スケジューラの作成 Private Sub Button1_Click( _ ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim obj As New Class1 obj.Test1() End Sub ' 毎日(または2日に)、1回だけ実行するタスク・スケジューラの作成 Private Sub Button2_Click( _ ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim obj As New Class1 obj.Test2() End Sub ' 毎日、3時間に1回、実行するタスク・スケジューラの作成 Private Sub Button3_Click( _ ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim obj As New Class1 obj.Test3() End Sub ' 毎週、月水金曜日に実行するタスク・スケジューラの作成 Private Sub Button4_Click( _ ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim obj As New Class1 obj.Test4() End Sub ' 毎月、15日に実行するタスク・スケジューラの作成 Private Sub Button5_Click( _ ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click Dim obj As New Class1 obj.Test5() End Sub ' 毎月、第2木曜日に実行するタスク・スケジューラの作成 Private Sub Button6_Click( _ ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click Dim obj As New Class1 obj.Test6() End Sub End Class |