How to make windows service keep running even when user logs out - c#

I have created a windows service using c# and deployed it in some machine. It's working well and good as long as the user is logged in. When user logs out, the service stop.
How can I make the windows service keep running irrespective of whether user is logged in or not? I want the service to run always until I go and stop it by myself.
User logs in as remote user to the machine (where the service is hosted) and can start or stop the service using an UI app. When he logs out, service stops.

Related

Screen Capture in windows service project [duplicate]

I have a windows service which gets the screenshots. But its creating only black screens. I know this happens because of session 0 isolation. I searched on internet and couldnt find any approved solution for this problem any working ideas will be really good.
1- Is there a way to change the session of a windows service and get the desktop screen of another user's session like session 1, session 2?
2- Is there a way to start a console application which runs in an another session other than session 0 from a windows service?
a windows service is designed to run also when there are no users connected, it works like a server process always up and listening, or up and doing something, or idle.
I think what you need is a client application which runs inside every logged user' session and eventually does the job then, if needed, communicates with the service to carry some job done.
I am saying here that instead of having the windows service running in another session than 0 you can create a small executable (probably with no UI at all) that starts up from the start up folder of all users at every user login. such application is then running inside the proper session and has access to it, it can get the screenshot then either store it somewhere itself or call some end points in your Windows Service (running always in session 0) and make the service to elaborate the screenshot taken from the client application of it.
this is the way I would do it, not trying some "magic" to tell Vista and 7 to start a service inside a session of a user that in the end is not logged in yet when the system starts.
Is there a way to change the session of a windows service and get the desktop screen of another user's session like session 1, session 2?
No.
Is there a way to start a console application which runs in an another session other than session 0 from a windows service?
This can be done but it's messy. It involves impersonation of the logged on user, manipulation of user tokens, and launching a process into a different session with CreateProcessAsUser(). This article describes what is needed.
As an aside, you don't want a console application because that will splat a console window on your screenshot. You just want a standard Windows app (using the GUI subsystem) but one that does not show any visible windows.

Windows Service hosting WCF - Can I Show an Windows Form on Stop Service?

I have one Windows Service in WCF running under admin user , this is not the logged user.
When the User will to stop de service, I want to show an Windows Form with a alert.
This is possible?
The issue you will encounter I think is to do with Windows sessions and which user is currently logged in. Popping a window might well work but won't necessarily show unless you are logged in as the same user that the service is running in the context of.
Another solution may be to create a system tray application that monitors the state of the service and communicates through windows messages or another mechanism. You could certainly have a timer there that confirms that the service in question is still running every X seconds, but that will be somewhat resource intensive.
Are there other mechanisms that would work for the communication such as email?

how to keep running C# application after windows log off

I wrote a C# application using RESTful web services. This application should interact periodically with our server application. I want this program to keep running in the background even if the user logs out of the computer. How can I do this in C#?
If you don't want your application to stop when the user logs out, then the application can't be running in the user's session (really a WinStation). That implies your app needs to run in a different session. The sessions that don't logout are service sessions. Run Task Manager and add the Session ID column, and view all processes, and you'll see what I mean.
So your application needs to run as, or be launched by, a service.
In addition to the first answer don't keep the service running under the specific user account. If you do so then also it won't work if you logged off.

Prompt for password in a windows service

How can i prompt for password and user when someone tries to stop a windows service?
It cannot be done.
Services are meant to run outside the user experiance and do not handle GUI interactions. This is something that is left up to the operating system to allow or disallow a user from stopping a service.
It cannot be done directly from the service. However, the service can be managed by another application if the service is set to interact with the desktop. So you could create a second application with a GUI that monitored the service. The service could set some value in shared state file when the services 'Stopping' event fires and wait until the monitoring app writes a confirmation value in the state file. Maybe not what you are looking for exactly but I think you could get the result that you want.
Ordinarily, there's no reason to do this. By default, only administrators can stop a service, and if the service can be stopped at all it makes no sense to ask an administrator for a password to do so: they're an administrator, so by definition they're entitled to do anything.
The one scenario that makes sense is if you want ordinary users to be able to stop the service if they know the password. That way, you can let someone stop the service without giving them administrative rights to the computer. (Even then, in most cases it would be simpler to change the permissions on the service to allow the user(s) in question the right to stop the service; but perhaps, for example, you want users to be have to phone a helpdesk to be given the password.)
The secret to making this work is that the service is entitled to stop itself for any reason without having received a stop request from the operating system. So you can just write a program that the users can run if they want to stop the service. The program accepts the password and sends it to the service over some form of IPC, such as a named pipe. If the password is correct, the service stops.
You could also configure the service so that it doesn't accept stop requests, in which case an administrator would also need the password in order to stop the service nicely. But that wouldn't stop them from stopping the service by killing the service process, or uninstalling the service and rebooting the computer.

Windows servce won't run if no user logged into server

I created a windows service that's basically a file watcher that wont run unless a user is logged into the machine its on.
The service is running on a Windows Server 2003 machine. It is designed to listen for excel files in a folder. When there is a excel file, it starts to send some information to a web service. When it's done, it copies the processed file to a archive folder.
Can anyone help me?
Best regards
Baris
Run it as a user that has rights to log on as service on the machine. Make sure this user has an access to the directory you watch.
What i always do in a scenario like that is give the user logon as batch job and logon as a service rights. 1 of these 2 will most likely fix your problem.
You can configure this in the local group policy editor as described here
Be aware though that if your computer is in a domain it is possible that the group policy gets pushed to the server every 15 mins so you might have to talk to a system admin to get things sorted.
When you actually only want to run when someone is logged in, do not use a service but an autostart application in that case.
If you have to be a service because of account privileges, the service may detect the current logins itself, but you may combine a service with a client (autostart) application that connects to the service. That way, you can also show tray incos, status informations and enable the user to control your service using the client application.
Using Win7 and higher, services themselves (running in session 0) can no longer display UI interactions on the user's desktop.
Keep in mind that there may be multiple users logged in on current operating systems...

Categories

Resources