简体   繁体   English

C#Windows服务

[英]c# windows service

I have a running windows service. 我有一个正在运行的Windows服务。 I would like to monitor ac# console application from the windows service . 我想从Windows服务 监视ac#控制台应用程序 My c# console application reads and writes in the console a sum, multiplication. 我的c#控制台应用程序在控制台中读写总和,乘法。 I would like to open and work with my console application using my windows service . 我想使用Windows服务打开并使用控制台应用程序 What should i write in OnStart() method in order to execute properly my c# code. 我应该在OnStart()方法中编写什么才能正确执行我的C#代码。

I did put in windows service the exe file (Process.start()) from my console application code but i have the error: console application stopped from working . 我确实从控制台应用程序代码中将exe文件(Process.start())放到Windows服务中,但出现错误: 控制台应用程序停止工作 WHY? 为什么? please help me:). 请帮我:)。 I know windows services do not work with GUI applications . 我知道Windows服务不适用于GUI应用程序 But I just want to open from my windows service another application (my exe that contains my console app). 但是我只想从Windows服务中打开另一个应用程序(包含控制台应用程序的exe)。 THX 谢谢

Can someone give me a little eq that works?:). 有人可以给我一些可行的公式吗? Thx 谢谢

It's not just GUI applications that don't work with Windows Services. 不只是GUI应用程序不能与Windows Services一起使用。 Console applications have the same problem. 控制台应用程序具有相同的问题。 They still run as a user-mode process, to which a Windows Service does not have access. 它们仍以Windows服务无法访问的用户模式进程运行。 Services run in a completely isolated process space called Session 0. They don't have access to the desktop of any user account, and they can't interact with applications running on those desktops. 服务在称为会话0的完全隔离的进程空间中运行。它们无法访问任何用户帐户的桌面,也无法与在这些桌面上运行的应用程序进行交互。

Aside from that, console applications still show a user interface. 除此之外,控制台应用程序仍显示用户界面。 The command prompt window that pops up on the screen still counts, even if it's not graphical. 屏幕上弹出的命令提示符窗口仍算在内,即使它不是图形的。 Launched from a Windows Service, it has no place to pop up. 从Windows服务启动,它没有弹出的位置。

This is a security feature of Windows Vista and later. 这是Windows Vista和更高版本的安全功能。 The "Enable service to interact with desktop" checkbox doesn't work anymore, and it hasn't since Windows XP. “启用服务与桌面交互”复选框不再起作用,并且自Windows XP以来一直没有。 You weren't supposed to use it there, either. 您也不应该在那里使用它。 Isolation protects Windows Services from attacks that originate in application code. 隔离可保护Windows Services免受源自应用程序代码的攻击。

Like almost every other time I see people asking questions like this, it's not clear why you need to use a Windows Service in the first place. 就像几乎每隔一次我看到人们问这样的问题一样,不清楚为什么首先需要使用Windows服务。 The simpler option is to create a Windows application that doesn't show any forms, and run it in the background. 比较简单的选项是创建一个不显示任何形式的Windows应用程序,然后在后台运行它。 This creates an effect very similar to a Windows Service, but without the limitations you're experiencing. 这会产生与Windows服务非常相似的效果,但没有您遇到的限制。 Because it's still running in the current user's space, it can launch and control other user mode applications. 因为它仍在当前用户空间中运行,所以它可以启动和控制其他用户模式应用程序。

Related reading: 相关阅读:

Does your console application log any errors? 控制台应用程序是否记录任何错误? Perhaps try checking the error logs to see what might be the cause of the console app issue. 也许尝试检查错误日志以查看导致控制台应用程序问题的原因。 This question may guide you to an answer/fix: 该问题可能会指导您找到答案/解决方法:

Avoid Program Stopped Working in C# 避免程序停止在C#中工作

Your question has some issues, as you mention, most windows servers block services directly accessing the GUI, as well as a service runs as its own instance of a user rather than any the one logged in. As a result, accessing a user run application is difficult if not impossible (just as the same way it cant see your user directories) 正如您提到的那样,您的问题存在一些问题,大多数Windows服务器都阻止直接访问GUI的服务,并且服务以其自己的用户实例(而不是任何已登录的实例)运行。因此,访问用户运行的应用程序即使不是不可能也很难(就像无法看到您的用户目录一样)

What is more feasible is the reverse. 相反,更可行。

If you have a service which your console app wishes to interact with, it could announce its arrival, and then on connection the service can reply with instructions for your console app to carry out. 如果您有控制台应用程序希望与之交互的服务,则它可以宣布其到达,然后在连接时,该服务可以回复控制台应用程序执行的说明。 But, you would need to devise a structual method of doing so. 但是,您将需要设计一种这样做的结构方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM