1

Closed

Uncaught exception

description

'Elucidate.vshost.exe' (Managed (v4.0.30319)): Loaded 'Z:\Elucidate\Elucidate-Phase_I\Elucidate\bin\Debug\TimeSpan2.dll'
A first chance exception of type 'System.NullReferenceException' occurred in Microsoft.Win32.TaskScheduler.dll
 
Running on Win 7 x64, when using the editor to go through the pages that have been generated.
 
This is caused when clicking on the "Run Times" tab shown by the TaskEditDialog.ShowDialog()

file attachments

Closed Aug 3, 2012 at 3:37 PM by dahall
Fixed. Cause is mixing old .NET with new (>3.5) .NET assemblies.

comments

smurfiv wrote Jan 20, 2012 at 7:39 PM

Task that is used in the dialog is generated from this code block:
     using (TaskService ts = new TaskService())
     {
        // Create a new task
        // Create a new task definition and assign properties
        TaskDefinition td = ts.NewTask();
        td.Data = "Your data";
        td.Principal.UserId = user;
        td.Principal.LogonType = TaskLogonType.InteractiveToken;
        td.RegistrationInfo.Author = "Elucidate";
        td.RegistrationInfo.Description = "Performs the SnapRAID Sync command after a small delay after logon";
        td.RegistrationInfo.Documentation = "http://elucidate.codeplex.com/documentation";
        td.Settings.DisallowStartIfOnBatteries = true;
        td.Settings.Enabled = true;
        td.Settings.ExecutionTimeLimit = TimeSpan.FromHours(24);
        td.Settings.Hidden = false;
        td.Settings.Priority = System.Diagnostics.ProcessPriorityClass.Normal;
        td.Settings.RunOnlyIfIdle = false;
        td.Settings.RunOnlyIfNetworkAvailable = false;
        td.Settings.StopIfGoingOnBatteries = true;
        Version ver = ts.HighestSupportedVersion;
        bool newVer = (ver >= new Version(1, 2));
        // Create a trigger that fires 15 minutes after the current user logs on and then every 1000 seconds after that
        LogonTrigger lTrigger = (LogonTrigger)td.Triggers.Add(new LogonTrigger());
        if (newVer)
        {
           lTrigger.Delay = TimeSpan.FromSeconds(30);
           lTrigger.UserId = user;
        }
        // Create an action which opens a log file in notepad
        td.Actions.Add(new ExecAction("cmd", @"/k" + FormatSnapRaidCommandArgs("Sync"), null));
        // Register the task definition (saves it) in the security context of the interactive user
        return ts.RootFolder.RegisterTaskDefinition(taskName, td, TaskCreation.CreateOrUpdate, null, null,
                                                      TaskLogonType.InteractiveToken, null);
     }

dahall wrote Jan 21, 2012 at 6:42 AM

Using this exact code to create a task, and on Windows 7 x64, I am not able to reproduce this error with the most current source code (1.7.2).

smurfiv wrote Jan 21, 2012 at 9:13 AM

I used the released version which is 1.7.1.
Also it is not the task creation that is the problem, it's when the user goes to the tab that is displayed by the dialogShow that causes the exception.

What is the ETA for 1.7.2 ? Can I have an Alpha style download release to test ?

dahall wrote Jan 21, 2012 at 4:18 PM

I did test it against the "Run Times" tab and did not see the exception. I will attach a pre-release version of 1.7.2 here. Official release is a few weeks from now.

smurfiv wrote Jan 22, 2012 at 12:01 PM

Thanks for the file, but this does not seems to have fixed it.
When the above task code is run (TemplateFunction) there is no SnapRAID Sync in the Task Schedule Manager.
If it run a 2nd time then it will not crash.
Follow the included AVI that show the creation of the task via the template, a large pause while the showdialog is running, then me clicking each of the shown tabs until the runtimes, then the uncaught exception handler is thrown from inside the TaskScheduler.dll.
HTH's

smurfiv wrote Jan 22, 2012 at 12:05 PM

at Microsoft.Win32.TaskScheduler.TaskFolder.DeleteTask(String Name)
at Microsoft.Win32.TaskScheduler.TaskPropertiesControl.runTimesTab_Leave(Object sender, EventArgs e)
at Microsoft.Win32.TaskScheduler.TaskPropertiesControl.runTimesTab_Enter(Object sender, EventArgs e)
at System.Windows.Forms.TabPage.OnEnter(EventArgs e)
at System.Windows.Forms.TabControl.WmSelChange()
at System.Windows.Forms.TabControl.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.Control.WmNotify(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.TabControl.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at Elucidate.Form1.btnEdit_Click(Object sender, EventArgs e) in Z:\Elucidate\Elucidate-Phase_I\Elucidate\Form1.cs:line 283

dahall wrote Jan 23, 2012 at 4:43 AM

I added some better error handling and re-uploaded the assemblies. Please try and let me know if the exception persists.

smurfiv wrote Jan 23, 2012 at 4:39 PM

Tried the new Assemblies (They still state 1.7.2.0) and got "Error shows in tab now" (See picture).
Does not pass the exception back to the code which is better. Just need better handling in the GUI part as this is "Customer facing" :-)

dahall wrote Jan 23, 2012 at 7:51 PM

One more thing to try. Remove the line:
td.Principal.UserId = user;
and change the RegisterTaskDefinition line at the end to:
ts.RootFolder.RegisterTaskDefinition(taskName, td);

smurfiv wrote Jan 24, 2012 at 5:51 PM

Done as suggested,
Still the same result as in the "Error shows in tab now.png"

dahall wrote Jan 25, 2012 at 5:05 PM

I was hoping changing those two lines would prevent the exception. To hide the error text, set the ShowErrors property to false. Try new assemblies in zip file.

smurfiv wrote Jan 25, 2012 at 5:36 PM

This is still not working, because when the Editor is loaded and a user clicks on the OK button (Do not even have to go to other tabs for that) the exception (as already mentioned) is thrown back to the calling application, just now from a different place in the editor.dll:
at Microsoft.Win32.TaskScheduler.TaskFolder.RegisterTaskDefinition(String Path, TaskDefinition definition, TaskCreation createType, String UserId, String password, TaskLogonType LogonType, String sddl)
at Microsoft.Win32.TaskScheduler.TaskEditDialog.okBtn_Click(Object sender, 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.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at Elucidate.Form1.btnEdit_Click(Object sender, EventArgs e) in Z:\Elucidate\Elucidate-Phase_I\Elucidate\Form1.cs:line 303

smurfiv wrote Jan 26, 2012 at 6:44 PM

I have managed to implement a workaround for this that may help you understand what variables are not being initialised (That's what your errors are stating)

Goto the following Diff [http://elucidate.codeplex.com/SourceControl/changeset/changes/73650#Elucidate-Phase_I%2fElucidate%2fForm1.cs]
and scroll down to lines 319.
There you will see that if the template function is used, then the GetTask(taskName) is called again, in order to return a fully initialised Task that will then allow the edit's and okay buttons to be used correctly

dahall wrote Jan 27, 2012 at 6:14 AM

You must have missed one of my comments. If you will remove the following lines from GetTaskTemplate, you will get rid of your errors and the long delay in bringing up the editor and it will not change the functionality of your code. You cannot specify an interactive token and a user account together.
td.Principal.UserId = user;
td.Principal.LogonType = TaskLogonType.InteractiveToken;

smurfiv wrote Jan 27, 2012 at 7:57 AM

The template function was taken from your "Complex example" where those 2 lines are together. [http://taskscheduler.codeplex.com/wikipage?title=Examples&referringTitle=Documentation]
And yes I did perform the steps you requested.
I shall try again tonight, but the long delay is still there even when editing an already existing Task.

smurfiv wrote Jan 27, 2012 at 6:26 PM

I can confirm that commenting out
td.Principal.UserId = user;
in the template function and then using the task directly in the Edit dialog causes an exception to be thrown when pressing ok.
I can also confirm that the timings (Delay) are the same.

dahall wrote Jan 30, 2012 at 3:27 AM

I have copied the task creation code from your Elucidate code exactly and put it in my test harness on Win7 x64. The dialog launches in 2.4 seconds and does not error when making changes and hitting Ok. Below is the code I'm using:

const string taskName = "Test";
TaskDefinition td = ts.NewTask();
td.Data = "Your data";
td.RegistrationInfo.Author = "Elucidate";
td.RegistrationInfo.Description = "Performs the SnapRAID Sync command after a small delay after logon";
td.RegistrationInfo.Documentation = "http://elucidate.codeplex.com/documentation";
td.Settings.DisallowStartIfOnBatteries = true;
td.Settings.Enabled = true;
td.Settings.ExecutionTimeLimit = TimeSpan.FromDays(1);
td.Settings.Hidden = false;
td.Settings.Priority = System.Diagnostics.ProcessPriorityClass.Normal;
td.Settings.RunOnlyIfIdle = false;
td.Settings.RunOnlyIfNetworkAvailable = false;
td.Settings.StopIfGoingOnBatteries = true;
Version ver = ts.HighestSupportedVersion;
bool newVer = (ver >= new Version(1, 2));
// Create a trigger that fires 30 seconds after the current user logs on
LogonTrigger lTrigger = new LogonTrigger();
if (newVer)
{
lTrigger.Delay = TimeSpan.FromSeconds(30);
lTrigger.UserId = user;
}
td.Triggers.Add(lTrigger);
// Create an action which opens a log file in notepad
td.Actions.Add(new ExecAction("cmd", @"/k", null));
// Register the task definition (saves it) in the security context of the interactive user
ts.RootFolder.RegisterTaskDefinition(taskName, td, TaskCreation.CreateOrUpdate, null, null, TaskLogonType.InteractiveToken);

// Edit task
Task t = ts.GetTask(taskName);
TaskEditDialog editorForm = new TaskEditDialog();
editorForm.Editable = true;
editorForm.Initialize(t);
editorForm.RegisterTaskOnAccept = true;
editorForm.ShowDialog();

smurfiv wrote Jan 30, 2012 at 4:15 PM

Thanks for the reply
1) It will not error because you are using the workaround of finding the task [Task t = ts.GetTask(taskName);] rather than using the one generated from the RegisterTaskDefinition return.
2) I included a video of how long it takes on my Win7 x64 machine, and it still takes that long. What machine OS are you using ?

dahall wrote Jan 30, 2012 at 5:52 PM

I just changed it so I am using the Task returned from RegisterTaskDefinition and get the same results as before: No exception and quick display of dialog. There must be something else. I'm running Windows 7 (6.1.7601) x64 on an old Centrino2 processor with 4Gb RAM. Check to make sure you aren't disposing the task instance before running the dialog. It seems there was a place where you put the Task or TaskDefinition variable in a "using" statement.