I have a windows application where I have created a windows service and Task to serve the purpose of my application.
The task is being created with OnlogonTrigger true and it starts my application (.exe) which is inside the system32 folder.
The application is built in C# dot net framework 2.0 . My application is installed using WIX.
I am creating the task at LogonTrigger so that my task is being called at every time when some one logged into that system.
I have written the code using (TaskScheduler.dll) to create ScheduledTasks at the OnStart method of my service. The service as well as the Task are getting created. The below code is for your reference.
ScheduledTasks ts = new ScheduledTasks();
string taskName = "myTaskName";
Task td = null;
protected override void OnStart(string[] args)
{
string getFullPath = '';
getFullPath = "*****************";
try
{
td = ts.OpenTask(myTaskName);
if (td == null)
{
td = ts.CreateTask(myTaskName);
td.ApplicationName = getFullPath;
td.Comment = "eWatch for user PC";
string acct = GetProcessOwner("explorer.exe");
td.Flags = TaskFlags.RunOnlyIfLoggedOn | TaskFlags.Hidden;
td.SetAccountInformation(acct, (string)null);
td.Triggers.Add(new OnLogonTrigger());
td.Save();
}
td.Close();
ts.Dispose();
}
catch (Exception ex)
{
}
}
I have installed my set up with Win7 at some computers and checked that the Task is created at every machine but fails to run in a few when some one logged into the system.
I am sure that the task is created at every machine because I have dumped a file when OnStart method is being called. Even I have written some text at this file while each an every line is executed. After installation, I have checked the file and found that all the lines are perfectly written; but when some one logged into those machines, my Task fails to trigger and my set up(.exe) is also not being started / called.
Then I have uninstalled this application and deleted the Task using ScheduledTasks.DeleteTask("myTaskName") which I have written at OnStop method of my service. The below code is for your reference.
protected override void OnStop()
{
try
{
td.Close();
td.Dispose();
ts.DeleteTask(taskName);
ts.Dispose();
}
catch (Exception ex)
{
}
}
But the above code is not able to delete the task and when I have again installed the set up , the control does not insert inside the IF block of OnStart method above. My dump file is also the proof of my above comment.
I can't understand why my Task is not getting triggered in those WIN7 machines when some one Logged into those WIN7 machines, but this same set up is working at some WIN7 machines.
I really confused why my Task is not working what it is supposed to do.
Please help me in this regard.
Any kind of help will be highly appreciated.