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:
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);
else
Console.WriteLine("The task has a custom event trigger with the following " +
"XML search value: {0}", ((EventTrigger)td.Triggers[0]).Subscription);
}