Run windows application from Windows service

南楼画角 提交于 2019-12-08 06:20:58

问题


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.


回答1:


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.




回答2:


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;
        }


来源:https://stackoverflow.com/questions/33385338/run-windows-application-from-windows-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!