Reputation: 181
Well, Im trying to spawn a process from inside a thread spawned by a Windows Service. The goal being to read the output and printing to the window in focus -- where cursor is. Probably a bad idea, I don't know i'm not a c# programmer
Anywho spawning the process works when I call the function itself, but not when it's called inside a running service. Is starting a process inside a service possible?
Process proc = new Process();
proc.StartInfo.FileName = @"C:\file.bat";
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
String outputMessage = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
Upvotes: 2
Views: 4285
Reputation: 29342
If you need to interact between your GUI and show some sort of feedback to the user, why don't you implement a WCF interface over a named pipe. Host it in your service and provide a "status" API or even functions if required. That way you can provide a gui to any user on the system and the service stays clean.
But as @David Heffernan mentioned, its not really recommended to create gui from windows services, and from Vista onwards they create in the Session 0 context.
Upvotes: 0
Reputation: 613282
Starting a process from a service is certainly possible. However, services run inside session 0 and have no desktop. Interactive users run their desktops in a different session. Consequently it is very challenging to get a process up and running inside an interactive session when starting from a service.
For an illustration of the issues involved, and how tricky it is to get right, read this: Launching an interactive process from Windows Service in Windows Vista and later.
The simplest approach would be to separate your service into two distinct parts. One part runs as a service, and the other part runs as a windowless process in the interactive session. These two processes can communicate by whatever IPC mechanism you prefer. When the service wants to start a process it simply sends a message to its other half in the interactive session to request that the new process is started.
Upvotes: 8