1

I am creating a windows service app which will run in background and check xyz.exe is running or not , if app is closed then after some interval of time it will open the app automatically

I am using a timer inside the service app but some how my app is opening in background instead of foreground, can some one help what's gone wrong here

Here is my code in windows service.exe :

protected override void OnStart(string[] args)
    {
        WriteToFile("Service is started at " + DateTime.Now);
        timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
        timer.Interval = 15000; //number in milisecinds  
        timer.Enabled = true;
       
    }

    private void OnElapsedTime(object source, ElapsedEventArgs e)
    {
        Process[] processes = Process.GetProcessesByName("xyz");
        if (processes.Length == 0)
        {
            WriteToFile("xyz.exe is closed at " + DateTime.Now);
            
            try
            {
               
               var p = Process.Start(@"C:\MyAPP\xyz.exe");
               p.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
                
            }
            catch(Exception exc)
            {
                WriteToFile("exception at " + exc);
            }

            
        }
        else
        {

            WriteToFile("xyz.exe is Running at " + DateTime.Now);

        }
    }
Clemens
  • 123,504
  • 12
  • 155
  • 268
Nation
  • 31
  • 5
  • 2
    Windows services run when there are zero users logged in, when one user is logged in, when multiple users are logged in. They don't get access to *any* of the desktop sessions, *if* any even exist. By wanting to run one program full screen at all times, it sounds like you're trying to badly reimplement [Kiosk mode](https://learn.microsoft.com/en-us/windows/configuration/kiosk-single-app). – Damien_The_Unbeliever Jan 27 '23 at 13:14
  • 1
    I don't know if Damien's guess is correct. If so, you should check out Kiosk mode. If it is only that the app should be running in any user's context (best effort), then maybe the windows task scheduler is better suited? It can start applications _as the logged in user_ if and when a user happens to be logged in. If I remeber correctly it also has the option "if not already running". – Fildor Jan 27 '23 at 13:24
  • @Damien_The_Unbeliever I want to avoid Kiosk Mode for my project , I just want my app to run in foreground from windows service, even I have tried to run the cmd from windows service then run a command to open the app , still the app is showing in background instead of foreground – Nation Jan 31 '23 at 09:08
  • And that's because, again, services have *no* access to the desktop. You're running into a *deliberate* limitation, and you're not going to solve it by adding more steps. – Damien_The_Unbeliever Jan 31 '23 at 09:09
  • then kiosk mode is use for UWP or MS store apps, but I want my wpf app to run after interval of time if app is closed – Nation Jan 31 '23 at 09:52
  • I think this solution should work for you - https://stackoverflow.com/questions/4147821/start-a-windows-service-and-launch-cmd – Rougher Mar 07 '23 at 15:44

0 Answers0