Why does Windows Service not launch external App?

五迷三道 提交于 2019-12-02 20:02:39

问题


I am trying to get a Windows Service to launch an external application. When I start my service it doesn't load the application up.

There are no errors reported in the event view either. It just says the service started and stopped successfully.

The following is the OnStart and OnStop code:

public partial class TestService : ServiceBase
    {
        public Process App { get; set; }

        public TestService()
        {
            InitializeComponent();

            App = new Process();

        }

        protected override void OnStart(string[] args)
        {
            App.StartInfo.FileName = @"C:\Program Files (x86)\SourceGear\DiffMerge\DiffMerge.exe";
            App.Start();
        }

        protected override void OnStop()
        {
            App.Close();
        }
    }

回答1:


If you are running on Vista, Windows 7 or Server 2008 and your executable is a windows application (Not Command-Line), then it will not run due to Session 0 Isolation, meaning there are no graphical handles available to services in the newest Windows OS's.

The only workaround we have found is to launch an RDP Session, and then launch your application within that session even though that is far more complicated.




回答2:


Enclose this code in try-catch and add a small trick which allows you to attach the debugger to the service. It is likely to be a permissions problem, but you will get it in the catch block

protected override void OnStart(string[] args)
{
    Debugger.Launch(); //displays a pop up window with debuggers selection

    try
    {
        App.StartInfo.FileName = @"C:\Program Files (x86)\SourceGear\DiffMerge\DiffMerge.exe";
        App.Start();
    }
    catch(Exception ex)
    {
        //see what's wrong here
    }    
}


来源:https://stackoverflow.com/questions/6271252/why-does-windows-service-not-launch-external-app

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