2

I have created one windows application which should run every time in the desktop. For this we put the application in startup. But due to some exceptions or user closing the window application getting close.

To avoid this I had written windows service, it will check every one minute whether application is running or not. If it is closed windows service will start the application.

When i am debugging the windows service the application is running fine. But when i done setup of the service. Service is running every one minute but windows application is not opening.

code as follows:

void serviceTimer_Elapsed(object sender, EventArgs e)
        {
            try
            {
                bool isAppRunning = IsProcessOpen("App"); 
                if (!isAppRunning)
                {                    
                    Process p = new Process();
                    if (Environment.Is64BitOperatingSystem)                        
                        p.StartInfo.FileName = @"C:\Program Files (x86)\Creative Solutions\App\App.exe"; 
                    else                        
                        p.StartInfo.FileName = @"C:\Program Files\Creative Solutions\App\App.exe";
                     p.Start();


                }
            }
            catch (Exception ex)
            {
                WriteErrorLog("Error in service " + ex.Message);
            }
        }

Method to check instance is running or not:

 public bool IsProcessOpen(string name)
        {
            //here we're going to get a list of all running processes on
            //the computer
            foreach (Process clsProcess in Process.GetProcesses())
            {              
                if (clsProcess.ProcessName.Contains(name))
                {
                    //if the process is found to be running then we
                    //return a true
                    return true;
                }
            }
            //otherwise we return a false
            return false;
        }

Please can anyone help how to resolve this issue.

Hussain
  • 223
  • 1
  • 4
  • 18
  • 1
    You're solving the wrong problems here. If there's code that should always be running, in should be in a service in the first place. If the program is exiting because of exceptions - fix the faults. Your current "solution" sounds like it's just going to make things worse. – Damien_The_Unbeliever Oct 28 '15 at 07:41
  • I would try to restructure your code so that you can put the code that needs to be running all the time in a service. If your code needs a UI you could still have that as an application that can be opened and closed as required. You could communicate with the service using something WCF. – AndrewJE Oct 28 '15 at 08:12

2 Answers2

0

Try looking at this question:

How can a Windows service execute a GUI application?

In your case with P/Invoke

However as stated, this is a poor design choice.

Required code, should run in the service using SCM and any configuration required or interaction with the service should be placed in a seperate client application communicating through .. whatever you like.

Community
  • 1
  • 1
Jack Andersen
  • 1,059
  • 2
  • 8
  • 8
0

This can be achieved simply by:
1)creating one console application.
2)Setup and deployment of Console application by making Output type as Windows Application from properties.

Code as follows:

static void Main(string[] args)
        {
            Timer t = new Timer(callback, null, 0, 60000);
            Thread.Sleep(System.Threading.Timeout.Infinite); 
        }

        // This method's signature must match the TimerCallback delegate
        private static void callback(Object state)
        {
            try
            {
                bool isAppRunning = IsProcessOpen("APPName");
                if (!isAppRunning)
                {
                    Process p = new Process();
                    string strAppPath;
                    strAppPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) + @"\FolderName\AppName.exe";                    
                    System.Diagnostics.Process.Start(strAppPath);                    
                }
            }
            catch
            { }
        }

        public static bool IsProcessOpen(string name)
        {
            //here we're going to get a list of all running processes on
            //the computer
            foreach (Process clsProcess in Process.GetProcesses())
            {
                if (clsProcess.ProcessName.Contains(name))
                {
                    //if the process is found to be running then we
                    //return a true
                    return true;
                }
            }
            //otherwise we return a false
            return false;
        }
Hussain
  • 223
  • 1
  • 4
  • 18