开发者

How can a WPF application be launched before I logon to Windows?

I would like to run WPF application before logging in to Windows. It shouldn't do any of existing UI just call ApplicationStartup event - it would trigger the functions I need.

Currently I'm using a windows service in conjunction with app, so service runs before logging in.

The downside now is that my app doesn't know what the service is doing without polling it.

And this is a requirement as machines can crash and restart itself, but开发者_运维知识库 Windows should still be running application anyway after boot up.

Is there any way?


No, this is not possible. WPF applications are user-mode applications, so they must be run within the context of a logged-in user. Since Windows is a multi-user operating system, it is possible for the OS to be running, but no users to be logged in. In those situations, there is no place for your application to run.

Windows services are the only thing that can do this, and that's precisely because they do not run under a particular user context. In fact, this same feature is often a source of frustration for developers who want to create a Windows service that interacts with the user. That's not possible (at least not as of Windows Vista, which patched all the security holes present in prior versions).

Alternative solutions:

  • Add your application to the default user's "Startup" folder, which will force it to launch each time that a user logs in to the machine.

    In this scenario, the only time it will not be running is when no users are logged in, in which case it's sort of hard to imagine that your app/service is actually doing anyone any good.

  • Configure each machine to automatically log in as a particular user whenever Windows starts up. Combining this with the above, you work around the fact that the computer could ever be left without a user logged in, even immediately after a crash/restart.

Although the pattern of having a separate user-mode application (WPF, WinForms, or whatever) that can show a UI control the Windows service running in the background is quite a common one. I'm not sure what you think is wrong with that setup. There's nothing wrong with polling the service when necessary for information. Services by definition do not require user intervention. Separating the part that requires user intervention out into a separate app is not only the typical practice, but necessary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜