Search code examples
c#windows-servicesbackground-task

Services or background Windows application


I wanted to create an application that will monitor application activities on my laptop and send information to my mobile phone.

My mobile phone will the interpret the message, then show options for me, then I'll select the option and send a response to the laptop. At the moment I'm thinking of sending this message through WiFi, Bluetooth, or TCP.

The question i have is around whether to create a service or a background windows application. I want it to be light so that it won't be using too much CPU or battery.

I have developed a background application before but not a Service. The application will be on all the time. I've looked at Services but not sure if it can interact to the current active window.


Solution

  • The answer depends on how your application will "monitor application activities on your laptop".

    Here are some important facts relevant to your situation:

    1. Each application running on your computer operates in a session — an entity holding processes and other system objects on behalf of a user who has logged in to the system. When you login to your PC, a new session is created for you. You will end up in Session 1, or Session 2, etc. (You can see each application's session in Task Manager by adding the "Session ID" column on the "Details" tab).

    2. When your PC boots, Windows automatically creates Session 0 to hold system processes and objects. All Windows Services run in Session 0.

    3. A Windows application (including a service) cannot easily interact with another application running in a different session. Basic API functions (like EnumWindows or GetWindowText) simply do not work across sessions. You have to jump through several hoops (involving the CreateProcessAsUser function) to operate across sessions.

    So if you write a Windows Service, it will not have easy access the UI elements of "regular" applications running on your interactive desktop. If that is an issue, you should write a regular application that will run in the same session as the application(s) you wish to monitor.