运行 windows 来自 Windows 服务的申请

Run windows application from Windows service

我创建了一个 windows 应用程序,每次都应该 运行 在桌面上。为此,我们将应用程序置于启动状态。但由于某些异常或用户关闭 window 应用程序正在关闭。

为了避免这种情况,我编写了 windows 服务,它会每隔一分钟检查一次应用程序是否 运行ning。如果关闭 windows 服务将启动应用程序。

当我调试 windows 服务时,应用程序 运行 运行良好。但是当我完成服务设置时。服务每分钟 运行 一次,但 windows 应用程序未打开。

代码如下:

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

检查实例的方法是 运行ning 与否:

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

谁能帮忙解决这个问题。

试着看看这个问题:

How can a Windows service execute a GUI application?

你的情况是P/Invoke

然而,如前所述,这是一个糟糕的设计选择。

必需的代码,应该 运行 在使用 SCM 的服务中,任何需要的配置或与服务的交互都应该放在一个单独的客户端应用程序中,通过..任何你喜欢的方式进行通信。

这可以简单地通过以下方式实现:
1)创建一个控制台应用程序。
2) 通过将输出类型设为 Windows 属性中的应用程序来设置和部署控制台应用程序。

代码如下:

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