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

后端 未结 1 1100
清酒与你
清酒与你 2020-12-22 07:35

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 n

相关标签:
1条回答
  • 2020-12-22 08:33

    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 讨论(0)
提交回复
热议问题