如何从 Windows 服务运行控制台应用程序?
我有一个用 C# 编写的 Windows 服务,我需要从中运行一个控制台应用程序。 控制台应用程序也是用 C# 编写的。
当控制台应用程序不是从 Windows 服务运行时,它运行良好。当它从 ws 运行时,它不会做任何它应该做的事情,因为它应该工作 10-20 秒,我在调试代码中看到立即执行。
我使用以下代码启动我的控制台应用程序:
proc.Start(fullPathToConsole, args);
proc.WaitForExit();
控制台的路径是正确的,当我尝试从 cmd 或仅在资源管理器(不带参数)中运行它时,它工作正常。但运行该服务后我没有看到任何效果。
我已经尝试转到服务属性并授予其访问桌面的权限并在系统和我的用户(也在服务属性中指定)下运行。一切都保持不变。
添加:我知道服务没有用户界面,但我不想要。我想要服务运行控制台应用程序。无需从中获取任何数据或使用此控制台(如 ui),只需运行它即可完成其工作。
更新我:发现运行 calc 或任何其他 Windows 应用程序很容易。但仍然无法运行 cmd 或任何控制台应用程序。实际上我需要在 XP SP2 和 Windows 2003 Server 上运行它。所以无论如何都不需要与 Vista 进行交互。
很高兴收到任何评论!
I have a windows service, written in c# and I need to run a console application from it.
Console application also written in c#.
Console application is running fine when it is run not from windows service. When it is ran from ws it doesn`t do anything it should and as it should work for 10-20 seconds I see in debug code is executed at once.
I`m starting my console app with the following code:
proc.Start(fullPathToConsole, args);
proc.WaitForExit();
the path to console is right and when I`m trying to run it from the cmd or just in explorer (without args) it works fine. But after running with the service I see no effect.
I already tried to go to service properties and give it access to desktop and run under both system and my user (also specified in service properties). All remains the same.
ADDITION: I know service do not have ui and I don't want one. I want service to run console application. No need to get any data from it or use this console like ui, just run it to do it`s job.
UPDATE I: discovered, that running calc or any other windows app is easy. But still can`t run cmd or any console app. Actually I need to run it on XP SP2 and Windows 2003 Server. So do not need to interact with Vista in anyway.
Would be glad to any comments!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您的控制台应用程序需要用户交互吗?如果是这样,那就是严重的禁忌,您应该重新设计您的应用程序。虽然有一些技巧可以在旧版本的操作系统中实现这种功能,但这肯定会在未来被打破。
如果您的应用程序不需要用户交互,那么您的问题可能与服务运行时的用户有关。尝试确保您以正确的用户身份运行,或者您正在使用的用户和/或资源具有正确的权限。
如果您需要某种用户交互,那么您将需要创建一个客户端应用程序并通过 rpc、套接字或命名管道与服务和/或子应用程序进行通信。
Does your console app require user interaction? If so, that's a serious no-no and you should redesign your application. While there are some hacks to make this sort of work in older versions of the OS, this is guaranteed to break in the future.
If your app does not require user interaction, then perhaps your problem is related to the user the service is running as. Try making sure that you run as the correct user, or that the user and/or resources you are using have the right permissions.
If you require some kind of user-interaction, then you will need to create a client application and communicate with the service and/or sub-application via rpc, sockets, or named pipes.
服务需要连接到服务控制管理器并在启动时提供反馈(即告诉 SCM“我还活着!”)。这就是为什么 C# 应用程序有不同的服务项目模板。您有两种选择:
Services are required to connect to the Service Control Manager and provide feedback at start up (ie. tell SCM 'I'm alive!'). That's why C# application have a different project template for services. You have two alternatives:
我有一个 Windows 服务,并且我将以下行添加到我的服务的构造函数中:
当我尝试运行此服务时,进行了 Process.Start() 调用,并且没有发生异常。但是,calc.exe 应用程序没有显示。为了使其正常工作,我在服务控制管理器中编辑了服务的属性,以启用与桌面的交互。完成此操作后,Process.Start() 按预期打开 calc.exe。
但正如其他人所说,与桌面的交互是微软所不赞成的,并且在 Vista 中基本上被禁用了。因此,即使你可以让它在 XP 中运行,我也不知道你是否也能在 Vista 中运行它。
I have a Windows service, and I added the following line to the constructor for my service:
When I tried to run this, the Process.Start() call was made, and no exception occurred. However, the calc.exe application did not show up. In order to make it work, I had edit the properties for my service in the Service Control Manager to enable interaction with the desktop. After doing that, the Process.Start() opened calc.exe as expected.
But as others have said, interaction with the desktop is frowned upon by Microsoft and has essentially been disabled in Vista. So even if you can get it to work in XP, I don't know that you'll be able to make it work in Vista.
我使用这个类:
I use this class:
在 Windows 服务中运行任何应用程序(例如“.exe”)都是很奇怪的,因为算法不是那么有效。
Running in Windows Services any application like for example ".exe" is weird to do because the algorithm is not that effective.
从 Windows Vista 开始,服务无法与桌面交互。您将无法看到从服务启动的任何窗口或控制台窗口。请参阅此MSDN 论坛主题。
在其他操作系统上,服务选项中有一个可用的选项,称为“允许服务与桌面交互”。从技术上讲,您应该为未来进行编程,并且应该遵循 Vista 指南,即使您不在 Vista 上使用它。
如果您仍想运行不与桌面交互的应用程序,请尝试指定进程不使用 shell。
看看这是否有效。
首先,您通知 Windows 该程序不会使用 shell(在 Vista 中无法访问该 shell)。
其次,您将所有控制台交互重定向到内部流(请参阅process.StandardInput 和process.StandardOutput。
Starting from Windows Vista, a service cannot interact with the desktop. You will not be able to see any windows or console windows that are started from a service. See this MSDN forum thread.
On other OS, there is an option that is available in the service option called "Allow Service to interact with desktop". Technically, you should program for the future and should follow the Vista guideline even if you don't use it on Vista.
If you still want to run an application that never interact with the desktop, try specifying the process to not use the shell.
See if this does the trick.
First you inform Windows that the program won't use the shell (which is inaccessible in Vista to service).
Secondly, you redirect all consoles interaction to internal stream (see
process.StandardInput
andprocess.StandardOutput
.我之前已经成功完成过此操作 - 我家里有一些代码。当我今晚回家时,我将使用启动控制台应用程序的服务的工作代码更新此答案。我想我会从头开始尝试这个。这是我编写的一些启动控制台应用程序的代码。我将其安装为服务并运行它,它工作正常:cmd.exe 启动(如任务管理器中所示)并持续 10 秒,直到我向它发送退出命令。我希望这对您的情况有所帮助,因为它确实按预期正常工作。
I've done this before successfully - I have some code at home. When I get home tonight, I'll update this answer with the working code of a service launching a console app.I thought I'd try this from scratch. Here's some code I wrote that launches a console app. I installed it as a service and ran it and it worked properly: cmd.exe launches (as seen in Task Manager) and lives for 10 seconds until I send it the exit command. I hope this helps your situation as it does work properly as expected here.
Windows 服务没有 UI。您可以使用此问题中显示的代码将控制台应用的输出重定向到您的服务。
Windows Services do not have UIs. You can redirect the output from a console app to your service with the code shown in this question.
正如 pierre 所说,没有办法为 Windows 服务提供用户界面(或者没有简单的方法)。在这种情况下,我所做的就是拥有一个设置文件,无论服务运行的时间间隔如何,都可以从服务中读取该设置文件,并拥有一个独立的应用程序来更改设置文件。
As pierre said, there is no way to have a user interface for a windows service (or no easy way). What I do in that kind of situation is to have a settings file that is read from the service on whatever interval the service operates on and have a standalone application that makes changes to the settings file.