I am trying to make a service that spawns a desktop application, and then watches to make sure it restarts again if it is closed. .
I would like it to basically spawn the process and then forget about it, allowing to act like a normal interactive application. (Apparently this is much easier to do in XP and before, but I need this for XP, Vista, and 7)
My problem now is that either it shows up invisible if I use process.start() with desktop interactive checked, and if I directly spawn a form it asks "Do you REALLY want to do this?!" and then the whole screen goes blank EXCEPT for my program.
I just want this to be an inoffensive background app. I have the app working 开发者_高级运维well, I just need to figure out how to spawn it from a service without all the trouble.
I am finding all of this stuff that says "Don't make services that have UI", but first off this was a requirement that was given to me. (Boss does not want it to be a scheduled task) Also, I noticed that the Task scheduler is itself a service, and it does not have any problem spawning user interactive applications. Why can't I do that too?
What am I doing wrong?
UPDATE: Thanks for all the useful info. Was able to convince my boss that this wasn't possible to achieve based on the session 0 isolation, so I marked that one as the answer and gave upvotes to everyone who gave useful info. Thanks again! :)
As of Windows Vista (and consequently Windows 7), services cannot directly interact with the user/desktop for security reasons. Dialogs and other forms that are spawned from a service are shown in Session 0 while your users are logged into Session 1. For more on this, read the Session 0 Isolation section here. In short, calling Process.Start()
from a Windows service on Windows 7 is going to cause issues.
You can still interact with the user. You just have to do so indirectly, as described here. I hope that points you in the right direction.
I would push back on why "Boss does not want it to be a scheduled task". You can set up a scheduled task that runs on every login, for example, which sounds like it's exactly what you need. You could also catch the Closing event and (from code) set up another scheduled task to relaunch yourself a few moments later.
What the boss probably doesn't get is that services are special. For example they are exempt from UAC. Therefore they can't talk to the user. If you want something that walks and talks like a service, but also talks to the user, the Windows 7 word for that is scheduled task.
Can't you make a program that is start at the login of the user and that will keep running until the session is closed ? I'd do that if I were you. You could start it with the login script of your users.
One problem with using services here is that they run without the user info and it also runs when nobody is logged on, hence the reason not to use UI in services.
精彩评论