如何从运行Windows服务控制台应用程序? [英] How to run console application from Windows Service?

查看:165
本文介绍了如何从运行Windows服务控制台应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗口服务,c#写的,我需要从它运行一个控制台应用程序。
控制台应用程序也写在C#。

I have a windows service, written in c# and I need to run a console application from it. Console application also written in c#.

当它不会从Windows服务运行控制台应用程序正在运行的罚款。当它从WS运行它不`吨做任何事情,它应该和它应该为10-20秒工作,我在调试code看到一旦执行。

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.

我真的开始我用下面的code控制台应用程序:

I`m starting my console app with the following code:

proc.Start(fullPathToConsole, args);
proc.WaitForExit();

安慰的路径是正确的,我真的时尝试从CMD或只是在资源管理器(无参数)运行它,它工作正常。但随着服务运行后,我看不出有什么效果。

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.

另:我知道服务没有用户界面,我不想要一个。我想服务运行控制台程序。没有必要从它那里得到任何数据或使用该控制台一样的UI,只要运行它做它`工作。

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.

更新我:发现,跑跑钙或任何其他Windows应用程序很容易。但仍不能老是运行CMD或任何控制台应用程序。其实我需要在XP SP2和Windows 2003 Server上运行。所以不需要反正与Vista进行互动。

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.

很高兴能有任何意见!

推荐答案

从Windows Vista开始,服务无法与桌面交互。你会不会能够看到,正在从服务启动任何窗口或控制台窗口。看到这个<一个href=\"http://social.msdn.microsoft.com/Forums/en-US/windowssdk/thread/f8f91e8f-5954-43a7-8bc4-80ed2ff1e3b1\">MSDN论坛主题。

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.

在其他的操作系统,也可用在一个名为允许服务与桌面交互的服务选项一个选项。从技术上讲,你应该编程的未来和应遵循的准则Vista中,即使你不使用它在Vista上。

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.

如果你仍然想运行,避免与桌面交互的应用程序,尝试指定过程中不使用的shell。

If you still want to run an application that never interact with the desktop, try specifying the process to not use the shell.

ProcessStartInfo info = new ProcessStartInfo(@"c:\myprogram.exe");
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.CreateNoWindow = true;
info.ErrorDialog = false;
info.WindowStyle = ProcessWindowStyle.Hidden;

Process process = Process.Start(info);

请参阅如果这样做的伎俩。

See if this does the trick.

首先,你通知Windows程序将不使用的shell(这是无法访问在Vista中的服务)。

First you inform Windows that the program won't use the shell (which is inaccessible in Vista to service).

其次,您重定向所有控制台交互,内部流(见 process.StandardInput process.StandardOutput

Secondly, you redirect all consoles interaction to internal stream (see process.StandardInput and process.StandardOutput.

这篇关于如何从运行Windows服务控制台应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆