1

I am using the pythoncom library in order to programmatically create and control a windows task. There is a setting under the generated xml for the task under Settings, it is Stop the task if it runs longer than: .... I can not seem to find the setting to change in the library. I have tried setting the task.SetMaxrunTime(-1) but that does not seem to disable this checkbox when I view it in the task properties.

Here is the example i have made so far:

import pythoncom
import time
from win32com.taskscheduler import taskscheduler

def create_task(name, cmd, parameters,):

    ts = pythoncom.CoCreateInstance(taskscheduler.CLSID_CTaskScheduler,None,
                                    pythoncom.CLSCTX_INPROC_SERVER,
                                    taskscheduler.IID_ITaskScheduler)


    ts.Delete(ts.Enum()[0]) # delete it since i am just testing
    task = ts.NewWorkItem(name)
    task.SetApplicationName(cmd)
    task.SetParameters(parameters)
    task.SetMaxRunTime(-1)
    task.SetPriority(taskscheduler.REALTIME_PRIORITY_CLASS)
    #task.SetFlags(taskscheduler.)
    task.SetAccountInformation('', None)
    ts.AddWorkItem(name, task)
    run_time = time.localtime(time.time() + 300)
    tr_ind, tr = task.CreateTrigger()
    tt = tr.GetTrigger()
    tt.Flags = 0
    tt.TriggerType = int(taskscheduler.TASK_EVENT_TRIGGER_AT_SYSTEMSTART)
    tr.SetTrigger(tt)

    pf = task.QueryInterface(pythoncom.IID_IPersistFile)
    pf.Save(None,1)
    task.Run()

    task = ts.Activate(name)
    exit_code, startup_error_code = task.GetExitCode()
    return win32api.FormatMessage(startup_error_code)

And the resulting task properties: Task Scheduler Settings tab for generated python task

How do I uncheck this Stop the task if it runs longer than: ... box from python?

aramnhammer
  • 135
  • 2
  • 9

0 Answers0