从 C# Windows 服务生成进程
好吧,我试图从 Windows 服务生成的线程内部生成一个进程。目标是读取输出并打印到焦点窗口(光标所在的位置)。可能是一个坏主意,我不知道我不是 c# 程序员,
当我调用函数本身时,任何生成进程的人都可以工作,但在正在运行的服务中调用它时则不行。是否可以在服务内启动进程?
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();
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从服务启动进程当然是可能的。但是,服务在会话 0 内运行并且没有桌面。交互式用户在不同的会话中运行他们的桌面。因此,从服务启动时,在交互式会话中启动并运行进程非常具有挑战性。
有关所涉及问题的说明以及正确处理的难度,请阅读以下内容:在 Windows Vista 和 Windows 中从 Windows 服务启动交互式进程稍后。
最简单的方法是将您的服务分为两个不同的部分。一部分作为服务运行,另一部分作为交互式会话中的无窗口进程运行。这两个进程可以通过您喜欢的任何 IPC 机制进行通信。当服务想要启动一个进程时,它只需在交互式会话中向其另一半发送一条消息,请求启动新进程。
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.
如果您需要在 GUI 之间进行交互并向用户显示某种反馈,为什么不通过命名管道实现 WCF 接口。将其托管在您的服务中,并根据需要提供“状态”API 甚至函数。这样您就可以向系统上的任何用户提供 GUI,并且服务保持干净。
但正如 @David Heffernan 提到的,实际上并不建议从 Windows 服务创建 gui,并且从 Vista 开始,他们在 Session 0 上下文中创建。
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.