【问题标题】:Run windows application from Windows service从 Windows 服务运行 Windows 应用程序
【发布时间】:2015-10-28 07:35:51
【问题描述】:

我创建了一个每次都应该在桌面上运行的 Windows 应用程序。为此,我们将应用程序置于启动状态。但由于一些异常或用户关闭窗口应用程序越来越接近。

为了避免这种情况,我编写了 Windows 服务,它会每隔一分钟检查一次应用程序是否正在运行。如果它关闭,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);
            }
        }

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

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

请任何人帮忙解决这个问题。

【问题讨论】:

  • 你在这里解决了错误的问题。如果有代码应该始终运行,那么 in 应该首先在服务中。如果程序因异常而退出 - 修复故障。您当前的“解决方案”听起来只会让事情变得更糟。
  • 我会尝试重构您的代码,以便您可以将需要一直运行的代码放在服务中。如果您的代码需要 UI,您仍然可以将其作为可以根据需要打开和关闭的应用程序。您可以使用 WCF 与服务进行通信。

标签: c# .net windows windows-services


【解决方案1】:

试试看这个问题:

How can a Windows service execute a GUI application?

在你的情况下使用 P/Invoke

但是如上所述,这是一个糟糕的设计选择。

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

【讨论】:

    【解决方案2】:

    这可以通过以下方式实现:
    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;
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-22
      • 1970-01-01
      相关资源
      最近更新 更多