0

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();
        }
    }
chobo
  • 31,561
  • 38
  • 123
  • 191
  • I don't see anything that would keep your service running. So it's possible that the process did start and then was immediately shut down when your service exited. – Matt Davis Jun 07 '11 at 20:36

2 Answers2

5

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.

Jbrand
  • 116
  • 1
  • http://stackoverflow.com/questions/5063731/is-there-any-way-to-start-a-gui-application-from-a-windows-service-on-windows-7 has some good comments on this. – Bueller Jun 07 '11 at 20:41
  • This seems to be the issue wince I am running Windows 7 and will need to port this over to Windows 2008 Server. From what I read the application has started but since it is in a different session the user will never see it. So, if I have an application that doesn't require user input it should work? – chobo Jun 07 '11 at 22:04
  • 1
    Yup, that's the case. I made a console app and loaded it via the windows service and it ran (you don't see console dialog flashing or anything), but it did write the events to the log file that I set in the app. – chobo Jun 07 '11 at 22:16
0

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
    }    
}
oleksii
  • 35,458
  • 16
  • 93
  • 163