Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having a TestProject with
a) Windows Form Application to Install/UnInstall/Start/Stop a Windows Service
b) A general Windows Service application also.

What i need is while Starting that Service i should invoke another .exe file to run as a process.
Is there any possible way to do so?

What I have tried:

Inside my SampleWindowSevice
Service1.cs
protected override void OnStart(string[] args)
{
  startTheProcess();
}

protected override void OnStop()
{
  stopTheProcess();
}  

public void startTheProcess()
{
 string path = "testExePath.exe";
 ProcessStartInfo startInfo = new ProcessStartInfo();
 startInfo.CreateNoWindow = false;
 startInfo.UseShellExecute = false;
 startInfo.FileName = path;
       try
        {
          using (Process exeProcess = Process.Start(startInfo))
          {
            exeProcess.WaitForExit();
          }
         }
       catch
        {
         // Log error.
        }
 }

 public void stopTheProcess()
 {
  foreach (var node in Process.GetProcessesByName("Dentrix.WebConnector.Api"))
  {
    node.Kill();
   }

}



Of course not working for sure. Is there any possible ways to do so?
Posted
Updated 18-Dec-20 4:40am

1 solution

You can start a process from a service, but that process cannot display any UI, since the service is not running in an interactive session.

You don't want the WaitForExit call in the service start code either, since it will cause your service, and potentially the service manager, to hang until the launched process exits.
 
Share this answer
 
Comments
Member 14978771 18-Dec-20 10:51am    
Normally this external .exe file i use to Install using NSSM service.I am trying to do that via Windows Service by expecting the working as same.
Richard Deeming 18-Dec-20 10:53am    
If it's displaying a UI, then you can't launch it from a service. Try using the task scheduler instead.
Member 14978771 18-Dec-20 11:03am    
Yes it's having a UI.
But I don't have any idea on task scheduler so far.
so my concern is, can we programmatically invoke this?
Dave Kreskowiak 18-Dec-20 12:17pm    
Not from a service.

Services run under a completely separate Desktop from the one the user sees. There is no way for a user to interact with the executable you start from a service.

You should NOT be launching an executable and waiting for it to terminate in OnStart. That should be done in the normal work of the service.
Member 14978771 18-Dec-20 12:47pm    
Ok Thank you.
But I still having confusions, since my requirement is in such a way. So if not bothering please assist me with a solution.
Like
a) can I able to create a Task Scheduler inside the Service OnStart() with the external .exe file.
or
b) can I create a Task Scheduler with the external .exe file separately and could invoke inside the Service OnStart().

If both ways are not possible how can I run those .exe file as a Service which should be restart on a daily basis also.

Please help...

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