I have:
- Simple WPF app, with 1 window created by Visual studio
- Windows service (created with topshelf) that starts that app
If I start app manually, it appears in task manager and shows form as it should.
But when it is started by windows service, app appears in task manager, but form isnt shown. What should it the reason?
App launch code in win service
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = _app.MainAppDirectory;
startInfo.Arguments = "";
process.StartInfo = startInfo;
process.Start();
2
Answers
You can’t do it.
Windows services
are not running undercurrent user
session. When you login to your Windows, you will get an active session including (Desktop, Environment variables, ..) and whenever you run an application (like a SimpleWindowsForm
orWPF
, …) it will runs under your active session. You can open up theTask manager
and navigate toDetail
tab to see information about the user who ran each Process (Check this for theWPF
app when it’s ran by your windows service).You can type
Services
on theStart menu
and open upServices
window and find your installed service. You can check a column namedLog on As
there. It’s the user account who runs yourWindows service
(it might beLocal System
orLocal Service
).It’s an interesting fact, you can write an application who runs even with no logged-in user.
I had the same task, 5 years ago, I tried to steal users session. Using
Impersonation
you are able to steal user token and run your application as an specific user. But still you are not able to see your Application on the desktop.Look for a different project type to fill your requirements. maybe mark your application as an startup app and hide it’s
Forms
from the user.Windows services run in session 0, which isolates services from user applications. This quote from the article gives more details on why they separated user applications from session 0.
There used to be a means to do what you are trying to do by making a interactive service but the first important section in that document states.
As far as I know, the checkbox is still there to make a service interactive but is only there for compatibility with installers that might have set it or read it.
That image is from my Windows 10 machine so the option to turn it on is there but like I said, I am under the impression it does nothing other than prevent applications which attempted to set it from breaking.
Typically the way I have seen applications work around this limitation is by writing a corresponding desktop app that is installed to launch at start and then uses some form of inter-process communication, like a socket to communicate with the app.