从服务C#运行Windows窗体应用程序(在Vista中和) [英] C# Run Windows Form Application from Service (and in Vista)

查看:175
本文介绍了从服务C#运行Windows窗体应用程序(在Vista中和)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的,需要作为服务运行,但也有用户交互的C#应用​​程序。据我所知,服务没有用户界面,等等,所以我已经瓜分了我的程序到Windows窗体应用程序,并可以相互通信的服务。

I am writing an application in C# that needs to run as a service but also have user interaction. I understand that services have no UI, etc, so I've divided up my program into a windows form application and a service that can communicate with each other.

我遇到的问题是,我需要的服务,以确保Windows窗体应用程序一直运行,并重新启动它,如果它不是。我能检测是否正在运行,以及在Windows上使用以下code重新启动2000 / XP:

The problem I'm having is that I need the service to make sure the windows form application is always running and restart it if it is not. I'm able to detect if it is running, and restart it with the following code on Windows 2000/XP:

System.Diagnostics.Process.Start("ExePath");

但在Vista上,它运行的新进程作为本地/系统的过程,是对用户不可见。难道这周围的人呀?是否有某种方式来检测哪些用户当前登录并运行新的过程,该用户?我并不需要考虑快速用户在此点的切换。东西 - 任何 - 基本就足够了。

but on Vista, it runs the new process as a Local/System process which is invisible to the user. Does someone way around this? Is there some way to detect which user is currently logged on and run the new process as that user? I don't need to account for fast-user switching at this point. Something - anything - basic would suffice.

我将是任何帮助或提示您对这个问题表示感谢。

I would be grateful for any help or tips you have on the subject.

编辑:
我需要澄清的是,我设置允许服务与桌面交互安装服务时选项。这就是允许它在2000 / XP的工作。但是,Vista中仍然有上述问题。

I need to clarify that I am setting the "Allow service to interact with desktop" option when the service is installed. This is what allows it to work on 2000/XP. However, Vista still has the aforementioned problem.

推荐答案

对于这样的事情是,如果用户需要与服务进行交互,就应该推出一个单独的应用程序的总体思路。如果你想帮助他们,你可以配置单独的应用程序通过放置一个快捷方式在开始菜单与windows启动。您也可以建立崩溃恢复到应用程序中,因此它可以自动重新启动。

The general idea for this sort of thing is, if the user needs to interact with a service, they should launch a separate application. If you want to help them out, you can configure that separate application to start with windows by placing a shortcut in the start up menu. You can also build crash recovery into your application so it can automatically restart.

您真的不应该依靠监控表单应用程序,如果没有人登录什么?如果多人登录何在?它只是变得混乱做事这样的。

You shouldn't really rely on monitoring the forms application, what if no one is logged in? What if multiple people are logged in? It just gets messy doing things this way.

具有服务只是坐在那里和广播听众是要走的路。当表单应用程序启动时,它可以通知它想要监听事件的服务。

Having the service just sit there and broadcast to listeners is the way to go. When the forms application starts it can notify the service it wants to listen to events.

这篇关于从服务C#运行Windows窗体应用程序(在Vista中和)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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