Sample code:
implemented with Microsoft.Win32.Scheduler version 1.42
but also reproducible with latest trunk;
class TaskRepository {
private TaskService taskService;
private string taskPrefix = "MyTask";
public TaskRepository()
{
taskService = new TaskService();
}
public void Save(Credentials credentials, string shellInstruction)
{
if(credentials == null)
throw new ArgumentNullException("credentials");
if(string.IsNullOrEmpty(shellInstruction))
throw new ArgumentNullException("shellInstruction");
var taskDefenition = taskService.NewTask();
taskDefenition.RegistrationInfo.Author = Strings.ApplicationName;
taskDefenition.Settings.ExecutionTimeLimit = TimeSpan.FromHours(24);
taskDefenition.Principal.RunLevel = Win32.TaskRunLevel.Highest;
taskDefenition.Actions.Add(new Win32.ExecAction(mainAppPath, shellInstruction, null));
taskService.RootFolder.RegisterTaskDefinition(
taskPrefix + DateTime.Now.ToString("ddMMyyyyHHmmss"),
taskDefenition,
TaskCreation.CreateOrUpdate,
credentials.Name,
credentials.Password,
TaskLogonType.Password, null);
}
public IEnumerable<TaskDefinition> Load()
{
var rootTasks = taskService.RootFolder.Tasks;
List<TaskDefinition> result = new List<TaskDefinition>();
foreach (var task in rootTasks)
{
if (task.Name.StartsWith(prefix, StringComparison.InvariantCultureIgnoreCase))
result.Add(task.Definition); // task.Definition <--- Exception thrown here
}
return result;
}
}
COMException: Interface not registered (Exception from HRESULT: 0x80040155)
at Microsoft.Win32.TaskScheduler.V2Interop.IRegisteredTask.get_Definition()
at Microsoft.Win32.TaskScheduler.Task.get_Definition()
at Scheduler.TaskRepository.Load() in ...\TaskRepository.cs:line 40
OS: Win7.
Tasks are created successfully, but while loading created tasks ComException is thrown on task.Definition property;
Comments: ** Comment from web user: dahall **
implemented with Microsoft.Win32.Scheduler version 1.42
but also reproducible with latest trunk;
class TaskRepository {
private TaskService taskService;
private string taskPrefix = "MyTask";
public TaskRepository()
{
taskService = new TaskService();
}
public void Save(Credentials credentials, string shellInstruction)
{
if(credentials == null)
throw new ArgumentNullException("credentials");
if(string.IsNullOrEmpty(shellInstruction))
throw new ArgumentNullException("shellInstruction");
var taskDefenition = taskService.NewTask();
taskDefenition.RegistrationInfo.Author = Strings.ApplicationName;
taskDefenition.Settings.ExecutionTimeLimit = TimeSpan.FromHours(24);
taskDefenition.Principal.RunLevel = Win32.TaskRunLevel.Highest;
taskDefenition.Actions.Add(new Win32.ExecAction(mainAppPath, shellInstruction, null));
taskService.RootFolder.RegisterTaskDefinition(
taskPrefix + DateTime.Now.ToString("ddMMyyyyHHmmss"),
taskDefenition,
TaskCreation.CreateOrUpdate,
credentials.Name,
credentials.Password,
TaskLogonType.Password, null);
}
public IEnumerable<TaskDefinition> Load()
{
var rootTasks = taskService.RootFolder.Tasks;
List<TaskDefinition> result = new List<TaskDefinition>();
foreach (var task in rootTasks)
{
if (task.Name.StartsWith(prefix, StringComparison.InvariantCultureIgnoreCase))
result.Add(task.Definition); // task.Definition <--- Exception thrown here
}
return result;
}
}
COMException: Interface not registered (Exception from HRESULT: 0x80040155)
at Microsoft.Win32.TaskScheduler.V2Interop.IRegisteredTask.get_Definition()
at Microsoft.Win32.TaskScheduler.Task.get_Definition()
at Scheduler.TaskRepository.Load() in ...\TaskRepository.cs:line 40
OS: Win7.
Tasks are created successfully, but while loading created tasks ComException is thrown on task.Definition property;
Comments: ** Comment from web user: dahall **
The base Microsoft library is not thread safe and is in a COM Apartment Threaded model which assumes a single thread. Each class must be used only with other classes instantiated in the same thread, including, and most importantly, the TaskService class.