Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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.

C#
ScheduledTasks ts = new ScheduledTasks();
string taskName = "myTaskName";
Task td = null;

protected override void OnStart(string[] args)
{
    string getFullPath = '';
    getFullPath = "*****************"; //Full path of the file
    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.

C#
protected override void OnStop()
   {

       try
       {
           td.Close();
           td.Dispose();
           // Remove the task we just created
           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.
Posted
Updated 20-Nov-14 1:15am
v2

1 solution

As with any problem, you need more data. My first suggestion would be to get an understanding if its a USER problem or a MACHINE problem.

Start collecting information about the successful runs versus the unsuccessful ones - an important data point is determining user account type (admin or standard?) for users and correlate if there are issues here. (Usually, it's a permissions thing.)

Machine based issues are a bit more complex - disparate OS patch levels, DOMAIN -vs- non-domain joined machines, group policy settings, .NET version, ACLS on directories and if login batch jobs run. The list is fairly long here and can get involved.

Take a bite at a time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900