locked
How do I start a interactive process from a windows service? RRS feed

  • Question

  • I am trying to start a process (for example notepad) from a windows service. I have used the below code:

        ProcessStartInfo info = new ProcessStartInfo("C:\\Windows\\System32\\notepad.exe");
        Process process = Process.Start(info);

    When I run it from the console it works fine but not when run as a windows service .

    I know that interaction with the desktop is not supported in windows services especially in vista and later.

    How do I get around this limitation?

    What I actually want to accomplish is that I need to start some applications when I start my machine like startup in c# code

    So I chose a windows service as the best way.

    How can I accomplish this.?
    Monday, July 20, 2015 4:20 AM

Answers

  • Simple answer:You don't. Services are not interactive. They were never suppsoed to. You aren't suppsoed to even ask the question.
    If you want interactivity services are a dead end.

    That being said for LEGACY Services that were designed dumbly ignoring this fact, there is a switch to allow interactive mode. Howover do not use it for future work. It will be removed.

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms683502%28v=vs.85%29.aspx

    If you want to work interactively on any kind of NT type OS, there are tons of more suiteable ways to autostart a programm.
    Like Autostart. Or the Windows Sheduler. Or the Registery.

    A common pattern is to have a service run in background with a controler programm started in interactive mode for this user. And both sides communicating via Loopback IP transfer or some other interprocess approach (named or unnamed pipes).

    • Edited by Christopher84 Monday, July 20, 2015 9:57 AM
    • Proposed as answer by Tim Roberts Wednesday, July 22, 2015 9:29 PM
    • Marked as answer by Kristin Xie Monday, July 27, 2015 3:03 AM
    Monday, July 20, 2015 9:57 AM

All replies

  • Can i get some suggestions
    Monday, July 20, 2015 8:56 AM
  • Simple answer:You don't. Services are not interactive. They were never suppsoed to. You aren't suppsoed to even ask the question.
    If you want interactivity services are a dead end.

    That being said for LEGACY Services that were designed dumbly ignoring this fact, there is a switch to allow interactive mode. Howover do not use it for future work. It will be removed.

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms683502%28v=vs.85%29.aspx

    If you want to work interactively on any kind of NT type OS, there are tons of more suiteable ways to autostart a programm.
    Like Autostart. Or the Windows Sheduler. Or the Registery.

    A common pattern is to have a service run in background with a controler programm started in interactive mode for this user. And both sides communicating via Loopback IP transfer or some other interprocess approach (named or unnamed pipes).

    • Edited by Christopher84 Monday, July 20, 2015 9:57 AM
    • Proposed as answer by Tim Roberts Wednesday, July 22, 2015 9:29 PM
    • Marked as answer by Kristin Xie Monday, July 27, 2015 3:03 AM
    Monday, July 20, 2015 9:57 AM
  • A Windows service is a very bad way to start applications when you start your machine.

    I suggest you forget the windows service approach.

    Either

    Add shortcuts to your programmes to the startup folder. On win 8+ that'll be like:

    C:\Users\<YourUser>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

    Where youruser is your user name.

    Or

    Add an entry to the registry:

    HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run

     

    Monday, July 20, 2015 10:07 AM