从Windows服务运行Windows应用程序 [英] Run windows application from Windows service

查看:70
本文介绍了从Windows服务运行Windows应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Windows应用程序,该应用程序应每次在桌面上运行.为此,我们将应用程序启动.但是由于某些异常或用户关闭窗口应用程序而关闭.

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.

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

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.

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

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.

代码如下:

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)创建一个控制台应用程序.
2)通过从属性中将输出类型设置为Windows应用程序,来设置和部署控制台应用程序.

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.

代码如下:

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

这篇关于从Windows服务运行Windows应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆