This project has moved and is read-only. For the latest updates, please go here.

Application error when editing new custom event trigger

Aug 28, 2012 at 1:07 PM

Steps for reproducing the error:

1. Click 'Triggers' tab
2. Click 'New' button
3. Select 'On An Event' from drop down list.
4. Select 'Custom' radio button
5. Type 'test' -> 'OK'

Data at the root level is invalid. Line 1, position 1.

-------------------------- Technical Information Follows --------------------------

Exception Details:
Exception type:  System.Xml.XmlException
Message:         Data at the root level is invalid. Line 1, position 1.
Source:          System.Xml
Target Site:     Throw

Stack Trace:
   at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
   at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
   at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
   at System.Xml.XmlReader.MoveToContent()
   at Microsoft.Win32.TaskScheduler.EventTrigger.GetBasic(String& log, String& source, Nullable`1& eventId)
   at Microsoft.Win32.TaskScheduler.EventTrigger.V2GetTriggerString()
   at Microsoft.Win32.TaskScheduler.Trigger.ToString()
   at Microsoft.Win32.TaskScheduler.TaskPropertiesControl.AddTriggerToList(Trigger tr, Int32 index)
   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.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Aug 28, 2012 at 6:26 PM

I have added error handling to catch when an invalid event string is supplied to the EventTrigger.Subscription property. To avoid this exception, and a failure when the task tries to execute, you should use valid event XML syntax here. See this link for more information.

Aug 28, 2012 at 6:42 PM

Thanks for the swift response, David!

Apr 28, 2014 at 5:57 AM
I get error when I use EventTrigger.GetBasic(). However if I use EventTrigger.SetBasic() I don't get any error. Can anyone tell me what exactly to write in .GetBasic() method
Apr 28, 2014 at 4:25 PM
Edited Apr 28, 2014 at 4:25 PM
curtishagen also posted the following as part of his question:

Hi, I am using some part of the above code
if (preWin7)
   // Create a trigger that will fire on a system security event
   EventTrigger eTrigger = (EventTrigger)td.Triggers.Add(new EventTrigger());
   eTrigger.SetBasic("Security", "VSSAudit", 25);
   eTrigger.ValueQueries.Add("Name", "Value");
eTrigger.SetBasic("Security", "VSSAudit", 25); creates a duplicate value in the Log and Source of task scheduler, which doesn't work i-e execute notepad at my end. Is there anyway I can use eTrigger.GetBasic("Security", "VSSAudit", 25);. I tried using it, it gives me error as method has invalid arguments and it should be used with "out" keyword. Thanks!
Marked as answer by dahall on 9/1/2015 at 10:06 AM
Apr 28, 2014 at 4:35 PM
Edited Apr 28, 2014 at 4:43 PM
First of all, you must be running the executable with an account that has sufficient permissions to watch events and create tasks (usually an Admin) or, if debugging, run Visual Studio as Administrator. You also must be running the task on a Vista or later system.

The EventTrigger has a constructor with the same parameters as the SetTrigger method. Either will create a simple event query for the trigger that will execute the task when a new event from the specified log, with the specified source and ID, is added. To be honest, I have no idea what adding ValueQueries does. The documentation is inconclusive. However, I have confirmed that the above code will indeed fire the task when that event is added.

The SetTrigger (or equivalent constructor) are only for creating these single entity queries. For more complex queries, one must use the Subscription property. In fact, SetTrigger simply sets the Subscription property with a pre-defined XML snippet. GetBasic looks for that pre-defined XML snippet and, if found, parses out the values and returns them through it's method parameters. If you put more complicated XML in the Subscription property, GetBasic will fail as it no longer matches the pre-defined XML snippet.

GetBasic should be used when reading an already created task as follows:
Task t = ts.GetTask(taskName);
td = t.Definition;
if (td.Triggers[0] is EventTrigger)
   string log, source;
   int? id;
   if (((EventTrigger)td.Triggers[0]).GetBasic(out log, out source, out id))
      Console.WriteLine("The task has an event trigger looking for {0}/{1} " +
         "with event ID {2}", log, source, id.Value);
      Console.WriteLine("The task has a custom event trigger with the following " +
         "XML search value: {0}", ((EventTrigger)td.Triggers[0]).Subscription);
Marked as answer by dahall on 9/1/2015 at 10:06 AM