Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have an exe and a windows service, I will keep windows service running all the time. But, I want to make sure that service is started if exe is launched. So, I will make an entry to event viewer that application is started, upon this entry I want to start windows service.

What I have tried:

I have tried below code to start another exe. But, I want to start windows service
C#
private static void CreateEventScheduler()
        {
            using (TaskService ts = new TaskService())
            {
                TaskDefinition td = ts.NewTask();
                td.RegistrationInfo.Description = "Auto - Start Service";
                EventTrigger eTrigger = (EventTrigger)td.Triggers.Add(new EventTrigger());
                eTrigger.SetBasic("Application", "TestApplication", 12345);
                eTrigger.Enabled = true;
                //eTrigger.ExecutionTimeLimit = TimeSpan.Zero;
                td.Actions.Add(@"D:\Application\Demo Applications\Console\AppForScheduler\AppForScheduler\bin\Debug\AppForScheduler.exe");
                // Register the task in the root folder
                ts.RootFolder.RegisterTaskDefinition(TaskName, td);
            }
        }
Posted
Updated 31-Mar-17 2:56am

Have a read of this article: Build Windows Event Log Watcher Service Process to Export Event Log Entries as RSS Feed[^]

You will find that it has the code sample that you are looking for.
 
Share this answer
 
Well, I created task scheduler manually, this scheduler gets triggered when Windows App logs system event message. I.e. In Trigger tab of scheduler, Select "On an event" from "Begin the task" drop down, add source name, Add EventID. While logging a message through App, Source and EventID should be same as mentioned in the task scheduler.

In Action section, select "Start a program" option, in program/script textbox add "%windir%/system32/sc.exe", in "Add argument" textbox add start "Windows Service Name".

Once task created, right click task >> export, this will create an xml.

Added this XML in INNO SETUP as a part of deployment package, this will register event to the machine whenever application is installed.

Also, added event log on windows application OnStart event.

So, whenever application is started, OnStart event is fired, Log will be registered to System Event Log, with specific ID and Source. Once the log is entered, associated scheduler will be triggered, which will start Windows service

Below is c# code for loggin message to event viewer through OnStart event

C#
//Add reference to System.Diagnostics namespace
protected override void OnStartup(StartupEventArgs e)
{
  const string Source = "TestWindowsApp", Log = "Application", Event = "TestWindowsApp       Application Started";
  const int EventID = 16317;

if (!EventLog.SourceExists(Source))
   EventLog.CreateEventSource(Source, Log);
   EventLog.WriteEntry(Source, String.Format("{0}: {1}", Event, System.DateTime.Now),     EventLogEntryType.Information, EventID);
}
 
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