TaskEditDialog throws NullReferenceException on OK button click if RegisterTaskOnAccept is true

Topics: Errors
Mar 24, 2014 at 9:35 PM
TaskEditDialog editorForm = new TaskEditDialog( );
editorForm.Editable = true;
editorForm.RegisterTaskOnAccept = true;
editorForm.Initialize( SelectedTask );
editorForm.ShowDialog( );
I know there are other questions about this issue but I've not found a solution in those other questions.

My application registers 3 x Tasks during installation. I want the tasks to run "Run whether user is logged on or not" so I need the user to enter their password at some stage. (Running the Tasks as SYSTEM will not work for my app).

I have a GridControl which loads the Tasks from a particular Folder as ItemsSource. Selecting a Task in the GridControl and Clicking the Editor button shows that Task in the TaskEditDialog.

With RegisterTaskOnAccept set to True clicking OK on the TaskEditDialog shows the NullReferenceException. Clicking Cancel doesn't.
Also using this method, selecting the "Run whether user is logged on or not" radio button shows the Password Dialog when it's OK button is clicked it shows the same NullReferenceException.

With RegisterTaskOnAccept set to false and manually registering the task after the TaskEditDialog has closed then everything works but then how can I get the Password Dialog to show using this method?

I've tried this on Windows 7 64bit which is on a Domain and on Windows 7 32bit which is not on a Domain, both with the same result.
Using .Net4.0 and Nuget to manage TaskSchedulerManagedWrapper (Version 2.0.0)

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Win32.TaskScheduler.TaskFolder.RegisterTaskDefinition(String Path, TaskDefinition definition, TaskCreation createType, String UserId, String password, TaskLogonType LogonType, String sddl)
   at Microsoft.Win32.TaskScheduler.TaskFolder.RegisterTaskDefinition(String Path, TaskDefinition definition)
   at Microsoft.Win32.TaskScheduler.Task.RegisterChanges()
   at Microsoft.Win32.TaskScheduler.TaskEditDialog.okBtn_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Mar 26, 2014 at 12:57 PM
I found a work around to the problem which may help someone else..
Moved my Database to C:\ProgramData\AppName.
Created my Tasks under the SYSTEM account.
Removed all ScheduleTask editing ability from my application because my application does not run as Administrator so SYSTEM's tasks will not show. Instead, I provided a simple button to open TaskSchd.msc.
These tasks run whether user is logged in or not and more importantly the user is never asked for their password.
Marked as answer by dahall on 4/3/2014 at 11:07 AM