System.InvalidCastException was unhandled
HResult=-2147467262
Message=Specified cast is not valid.
Source=Microsoft.Win32.TaskSchedulerEditor
StackTrace:
at Microsoft.Win32.TaskScheduler.TaskPropertiesControl.taskMultInstCombo_SelectedIndexChanged(Object sender, EventArgs e)
at System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e)
at System.Windows.Forms.ComboBox.WmReflectCommand(Message& m)
at System.Windows.Forms.ComboBox.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
InnerException:
I'm calling the UI Dialog as follows:
Microsoft.Win32.TaskScheduler.Task dct = null;
try
{
dct = util.CreateTask();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
if (dct != null)
{
TaskEditDialog ted = new TaskEditDialog(dct, true, true);
try { ted.ShowDialog(); }
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
Windows 10 Professional. Visual Studio 2012. Nuget packages installed Sep 2015. Any assistance gratefully received.
Comments: ** Comment from web user: dahall **
Thank you. I'm running tests now and will reply when I have a resolution. In the mean time, I have some suggestions for how to use the library that are a little more optimal. 1) Use GetTask instead of FindTask if you know the task is in the root folder. 2) You are setting some properties to their default values. 3) The TimeTrigger constructor allows for the start time. 4) The Actions.Add method has a shortcut for adding executables.
```
TaskService tsmgr = new TaskService();
var dct = tsmgr.GetTask(strings.DataCommunicator2Task);
if (dct == null)
{
string strThisProgram = Assembly.GetEntryAssembly().CodeBase.ToString();
strThisProgram = strThisProgram.Replace(@"file:///", @"");
strThisProgram = strThisProgram.Replace('/', '\\');
TaskDefinition td = tsmgr.NewTask();
// td.Settings.AllowDemandStart = true; Default value - not needed
td.RegistrationInfo.Description = strings.ExecuteTheDataCommunicator;
TimeTrigger tg = new TimeTrigger(DateTime.Now.AddHours2);
// tg.Enabled = true; Default value - not needed
tg.Repetition.Interval = TimeSpan.FromMinutes(5);
td.Triggers.Add(tg);
td.Actions.Add(strThisProgram);
try
{
tsmgr.RootFolder.RegisterTaskDefinition(strings.DataCommunicator2Task, td);
dct = tsmgr.GetTask(strings.DataCommunicator2Task);
}
catch
{
MessageBox.Show(strings.CouldNotCreateDataCommunicatorTask);
dct = null;
}
}
return dct;
```