Which Windows services can be disabled to speed up the system. Windows Services Service versus Application

Hello dear readers, today I would like to talk:

1. ABOUT windows serviceswhat it is, what it is for and which ones are responsible for what.

2. And how to improve the speed of your computer?

So what are these Windows services?

Services - applications automatically or manually launched by the system when windows startup and various tasks performed regardless of the user's status.

Open the list of services in several ways:

1. While holding windows button press R, a window will open, enter services.msc there

2. Start\u003e Control Panel\u003e Administrative Tools\u003e Services

3. Start\u003e right click mouse on my computer\u003e Management\u003e Services and Applications\u003e Services

As you can see there are a lot of them in Windows and after downloading, you can familiarize yourself what services exist and what each of them is responsible for.

Since services are applications, they therefore run and use some of the computer's resources. you can improve its performance. Let's see what you can disable.

What services can be disabled in Windows 7, 8

I did not make a list of those services that can be disabled. many services are individual. I just tried to describe each service and in what situations they can be disabled. If you need to disconnect something thoughtlessly, then just use it.

* BranchCache -The service caches online content. If you do not use your home network, you can disable it altogether.

* DHCP client -If you use the Internet, do not touch in any case. It is this service that assigns you an ip address.

* DNS clientIt is also a necessary service for using the Internet. Works with your DNS (serves for the right directions).

* KtmRm for Distributed Transaction Coordinator -system function of transactions. We leave it the same way.

* Microsoft .NET Framework -We leave all such services as they are. They are used for the normal operation of most applications.

* Parental Controls -Service for parental control... If not using, you can disable it.

* Plug-and-Play - serves for automatic recognition of changes in the system. For example, when you connect a USB flash drive, this service wakes up ... So we leave it as it is.

* Quality Windows Audio Video Experience -transmission of audio and video over the network in real time. Not needed only if there is no network (or Internet), in other cases we leave it.

* Remote Desktop Configuration -For remote desktop. If you don't use remote connections, disable.

* Superfetch -A useful function that works with the cache. Accelerates windows work, so we leave.

* Windows Audio -Controls the sound. If you do not need sound, turn off. In other cases, we leave it.

* Windows CardSpace -unnecessary and insecure service. For this we turn off.

* Windows Driver Foundation - User-mode Driver Framework -for normal operation of the drivers, do not touch. Let it remain as it is.

* Windows Search -Indexing files for search. If you do not use it and there is time to wait until the file is found, then disable it. On ssd, be sure to disable it!

* WMI Performance Adapter -needed for services that require wmi, set manually. If some applications are needed, they will launch themselves)

* WWAN Auto Config -service for use mobile internet... If you use a usb modem, a sim card in your laptop, then do not disconnect.

* Offline files -helps to work offline with inaccessible files that were loaded before. We put it manually.

* Network Access Protection Agent -We put it manually, because if necessary, the service will start if some program requests the necessary information.

* ANDiPsec Policy Gent -It is needed if there is a network and the Internet.

* Adaptive brightness control -We leave if there is a light sensor.

* Windows Backup -Disconnect if not in use. But it's better to read about archiving in windows you never know, you will use it.

* Windows Biometric Service -only needed when using biometric devices. In other cases, disable it.

* Windows Firewall -To be honest, I always turn it off. I have nothing to steal from me) And if they encrypt the data, I will restore it) But I would advise you to get, for example, a Kaspersky Internet security, which has both an antivirus and a firewall. And turn off this nafig, tk. he sometimes blocks what is not needed) In general, he monitors the security of your computer and closes the ports so that thieves cannot get into your computer)

* Computer browserNo need for a home network. Manually.

* Web client -Tedious if there is no internet. Serves for working with files on the Internet. We leave.

* Virtual disk -Service for working with storage devices. We put it manually.

* IP Helper -Works with protocol version 6. I always disable it myself, so you can disable the service altogether.

* Secondary login -Set manually, because some games or programs will turn it on as needed.

* Grouping network participants -Needed for a home group. Set manually, you never know ...

* Disk Defragmenter -In principle, it does not interfere. You can leave it on or off. If you disable it, I recommend doing it once a month. And for ssd drives, turn it off altogether!

* Dispatcher automatic connections remote access -We put it manually. Needed for remote connections.

* Print Manager -Needed if you have something to print from. In other cases, disable it.

* Remote Access Connection Manager -manually. I disconnected it once and could not create a connection. So it's better by hand.

* Desktop Window Manager Session Manager -If you are not using transparency from Aero, then you can turn it off, it will give a big boost.

* Network Participant Identity Manager -Better to put it manually.

* Credential Manager -Better by hand. Stores your data, such as usernames and passwords.

* Security Accounts Manager -Better to leave it as it is. If you disable this service, then all changes in the local security policy will be lost.

* Access to HID devices -Access to shortcuts... Disable, if some combinations stop working, then put it back.

* Windows Event Log -records all events. A useful tool for the advanced user. It is impossible to disconnect.

* Performance Logs and Alerts -system service, we leave it as it is.

* Software Protection -also the system service, we leave it as it is.

* Windows Defender -Spyware and malware protection. Install a normal antivirus and disable this service.

* CNG Key Isolation -Manually.

* Tools windows managementSystem service, without it, some applications may not work correctly, so it's better to leave.

* Application Compatibility Information -A useful thing, it helps start applications that refuse to work on your OS. We put it manually.

* Group Policy Client -We leave. Responsible for security policy settings.

* Changed Links Tracking Client -Tracking ntfs files is unnecessary. Disable.

* Distributed Transaction Coordinator -We put it manually.

* Cash windows fonts Presentation Foundation -We put it manually. Applications will launch it if necessary.

* SNMP Trap -Some programs will collect information about you. So disconnect.

* Remote Procedure Call (RPC) Locator -Manually, if necessary, applications will launch it.

* Routing and Remote Access -Need not. Disable.

* IPsec Key Modules for Internet Key Exchange and Authenticated IP -Not needed, but better manually.

* DCOM Server Process Launcher -System service, leave it as it is.

* NetBIOS over TCP / IP Helper -If there are no other computers on the network, then manually.

* Immediate windows connections - setup logger -Manually.

* SSDP Discovery -Leave it as it is. Required for new devices.

* Discovery of online services -Manually.

* Internet Connection Sharing (ICS) -Not needed if you are not sharing your internet on network connections.

* Determination of shell hardware -required for the autorun disk or flash drive dialog box. Whoever is comfortable, most need it. I left.

* The core TPM services are -Only needed to use TMP and / or BitLocker chips.

* Remote Desktop Services User Mode Port Forwarder -If you are not using remote connections, then it is not necessary. Better to put it manually.

* PpnP-X IP Bus Enumerator -Better to put it manually.

* Nutrition -Doesn't turn off. We leave.

* Task Scheduler -It is advisable to leave it as it is. now many programs use it.

* Media class planner -We leave for whom the sound is important.

* Support for the Problem Reports and Resolutions Control Panel item -Manually.

* Smart Card Removal Policy -for smart card users, better manually.

* Homegroup provider -For home groups use. Better by hand.

* Wired Auto Tuning -Manually.

* Software Shadow Copy Provider (Microsoft) -Manually.

* Homegroup listener -Manually.

* PNRP protocol -We also leave it manually. Some applications can use the service.

* Feature Discovery Resources Publishing -Needed if you want to show your files to other computers over the network. If you don't want to, then manually or disable it.

* Work station -better to leave, because some applications use this service.

* Certificate Distribution -Better by hand.

* Extensible Authentication Protocol (EAP) -Manually.

* Windows Event Collector -Manually.

* Application Details -Manually.

* Server -If the computer is not used as a server or is not using file and printer sharing, then disable it.

* Stream Sequencing Server -Disable if there is no home group.

* Network Login -Manually.

* Network connections -Leave it as it is. If there is no network or Internet, you can turn it off.

* COM + Event System -set manually. Applications that depend on this service will start it themselves if necessary.

* COM + System Application -Also manually.

* SSTP Service -We leave it as it is, we need a service if there is Internet on the computer.

* WinHTTP Web Proxy Auto Discovery Service -If you need the Internet, then leave it as it is.

* WLAN Auto Configuration Service -service for wireless networks... Accordingly, if they are not there, it is not needed.

* Basic Filtering Service -on the one hand, it is not needed (unless security is needed), but on the other hand, some programs may give errors. So let's leave.

* Tablet PC Input Service -if the screen is not touch, then it is not needed.

* Windows Time Service -needed to synchronize time with the Internet.

* Download service windows images (WIA) -You only need a service if you have a scanner. She is responsible for obtaining images from scanners and cameras.

* Microsoft iSCSI Initiator Service -We put it manually, if the programs need it, they will launch it themselves.

* Network Save Interface Service -Needed for normal network operation.

* Windows Font Cache Service -serves to improve performance, caches fonts and does not waste time downloading.

* FROMluzhba of the Media Center set-top box -If you are not using any prefixes, it is not needed.

* Block Level Archiving Engine Service -We put it manually. If a backup or restore is needed, the service will start itself.

* Service general access to Net.Tcp ports -Disabled by default. Needed if only the Net.Tcp protocol is needed.

* Player Network Sharing Service Windows MediaManually. It will be needed, it will turn on.

* Portable Device Enumerator Service -Serves to synchronize music, videos, etc. with removable media. I would put it manually. This is not always necessary.

* Windows Media Center Scheduler Service -It is necessary if you only watch programs in Windows Media Player.

* Bluetooth Support -Needed if there is Bluetooth.

* Diagnostic Policy Service -Needed to diagnose problems ... To be honest, she rarely helps. Therefore, you can experiment by disabling it. If necessary, turn it on.

* Program Compatibility Assistant Service -the service is needed to run programs that is incompatible with your OS. If not, set it manually.

* User Profile Service -Better to leave. It works with computer user profiles.

* PNRP Computer Name Publishing Service -Needed for home groups.

* Windows Error Logging Service -Logs errors. Better to put it manually.

* Windows Media Center Receiver Service -to watch TV and radio programs in the player.

* Connected Network Awareness Service -It is better to leave it as it is for normal network operation.

* Network List Service -It is also better to leave.

* SPP Notification Service -For licensing. Leave manually.

* System Event Notification Service -If you are not going to watch Windows messages, then you do not need it.

* Service remote control Windows (WS-Management) -Set manually.

* BitLocker Drive Encryption Service -Encrypts discs. If you are not using it, then it is better to turn it off.

* Application Layer Gateway Service -The service is only needed to work with the firewall. Manually.

* Cryptographic Services -Leave it as it is to install new programs.

* Remote Desktop Services -If you do not use remote desktops, then disable.

* Smart card -If you don't use them, then you don't need it.

* RPC Endpoint Mapper -The service is needed for incoming traffic. Nothing can be done with it. For this we leave.

* Windows Audio Endpoint Builder -If you need sound, leave it.

* Telephony -Leave manually. If necessary, it will start.

* Topics -Eat a lot of memory resources. Disconnect if not needed.

* Volume shadow copy -Creates restore points, backed up in the background. Set manually. If necessary, it will start.

* Link layer topology -Also manually. If necessary, it will start.

* Remote Procedure Call (RPC) -System service. Leave it as it is.

* Remote registry -Allows remote users to manipulate your registry. Disconnect.

* Application Identity -Manually.

* Diagnostic system node -Diagnosing problems. Set manually.

* Diagnostic Service Host -Also manually.

* Generic PNP Node -Set manually. Not all PnP devices.

* Application management -Set manually. The service allows you to configure policies for applications.

* Managing certificates and health key -Set manually, you need it, it will start itself.

* ActiveX Installer -Also manually. You will need to install such an object, it will start itself.

* Windows Installer -Installing .msi programs. Manually.

* Windows Modules Installer -Installs and removes components and updates. Manually.

* Fax machine -Only needed if there is a fax.

* Background Intelligent Transfer Service (BITS) -We leave it manually. Service helpful.

* Discovery function provider host -We leave it manually. You will need to start.

* Windows Color System (WCS) -Manually. The devices will need it, they will launch it.

* Security Center -Follows windows security... She pisses me off with her notifications. So it's up to you to turn off or not.

* Windows Update -One side useful function... It plugs holes in the system, updates drivers, but on the other hand, actively uses the Internet, memory resources, and if you turn off the computer during the update, the OS may crash. So you can also choose which is more important, security or performance.

* Encrypted file system (EFS) -For the safety of files. Better to leave it as it is manually.

I tried to present the entire list of services. By disabling some, you will improve the performance of your computer. You can also decide at your own discretion which ones are needed and which are not. For example, if there is no Internet, then you can safely cut half of it, if there is no printer, then you can also turn off a lot. Thus, depending on your needs, you can significantly revitalize your old computer.

Can a client application be run as a service? Not every console application will be able to run as a service, and GUI programs generally cannot work this way. But the ability to run the application as a service is still there, and the program with the original name will help us with this. Non-Sucking Service Manager.

NSSM is free and open source software and supports all operating systems Microsoftfrom Windows 2000 to. NSSM does not require installation, just download and unpack it. The distribution includes versions for 32- and 64-bit OS. You can take the program from the nssm.cc site, at the moment the latest stable version is 2.21.1, which I will use.

To demonstrate the capabilities of NSSM, let's try to run Notepad as a service on.

Service creation

To create a service named notepad launch the command console, go to the folder with the unpacked NSSM (for 64-bit Windows) and enter the nssm install notepad command, which opens the graphical NSSM installer window. To create a service, just specify the path to the executable file in the Path field and click the "Install service" button. Additionally, in the Options field, you can specify the keys required to start the service.

Also at the stage of creating a new service, you can specify some additional parameters.

The Shutdown tab lists the shutdown methods and timeouts used when the application is shutting down or crashing normally. When NSSM receives a stop command (for example, when an application is shutting down), it tries to stop the monitored application in a regular way. If the application does not respond, then NSSM can forcefully terminate all processes and subprocesses of this application.

There are four stages of application shutdown in total, and by default they will be used in this order:

At the first stage, NSSM tries to generate and send an event Ctrl + C.This method works well for console applications or scripts, but is not applicable for graphics applications;
The NSSM then identifies all windows created by the application and sends them a WM_CLOSE message to exit the application;
In the third step, NSSM calculates all threads created by the application and sends them a WM_QUIT message, which will be received if the application has a thread's message queue;
And as a last resort, NSSM can call TerminateProcess () to force the application to shut down.

It is possible to disable some or even all of the methods, however, different methods work for different applications and it is recommended to leave everything as it is in order to properly shut down the application.

By default, when the NSSM service crashes, it tries to restart it. On the "Exit actions" tab, you can change the automatic action in case of an abnormal application termination, as well as set a delay before the automatic restart of the application.

On the "Input / Output (I / O)" tab, you can set the redirection of application input / output to the specified file.

On the "Environment" tab, you can set new environment variables, or override existing ones.

You can also not use the graphical shell and immediately create a service in the console with the following command:

nssm install notepad ″ C: \\ Windows \\ system32 \\ notepad.exe ″

Service management

After creating the service using NSSM, go into the Services snap-in and find the notepad service. As you can see, it looks no different from other services, we can also start it, stop it or change the startup mode. Note, however, that nssm.exe is listed as the executable file.

And if we go into the Task Manager, we will see the following picture: NSSM is launched as the main (parent) process, the notepad service is launched as its child process, and the Notepad application is already running in this child process.

Removing a service

To remove the service, enter the nssm remove notepad command and confirm its removal. And by entering the command nssm remove notepad confirm, you can do without confirmation.

Starting a service interactively

The main difference custom application from the service lies in the fact that after starting the application may require additional actions from the user to continue working - for example, press a button or enter a command. To do this, you need to get access to it, which, as it turns out, is not so easy to do.

In order to start a service in interactive mode, you need to open its properties in the Services snap-in and check the "Allow interaction with the desktop" checkbox on the "Login" tab.

And then the miracles begin. For a service launched in interactive mode, the system opens a separate isolated session (session 0). You can only get into this session using the Interactive Services Discovery Service (ui0detect), which monitors the startup of interactive services on the computer and issues an alert. In Windows 7 \\ Server 2008 this service is active by default, and in Windows 8 \\ Server 2012 it is disabled and does not appear in the graphical Services snap-in (at least I did not find it there). Moreover, if you do find this mysterious service and try to start it, you will receive an error message.

And the fact is that to start it, you must allow the launch of interactive services on the computer. Therefore, we open the registry editor, find in the HKLM \\ System \\ CurrentControlSet \\ Control \\ Windows section a DWORD parameter with the name NoInteractiveServices and put its value in 0 .

Then open the PowerShell console and start the discovery service with the command:

Start-Service -Name ui0detect

After making sure that the discovery service is running, we restart the notepad service, and we get this window. We select the item "View message"

and we get into the zero session in which our application is running. Then we perform the necessary actions with it and go back.

This is an interesting solution for running applications as Windows services. Not the most beautiful, but quite true to its name

How to run the application as windows services



Can a client application be run as a service? In one of the articles, how to create a service Windows standard by means of OS. However, not every console application will be able to run as a service, and programs with a graphical interface, in principle, do not know how to work in this way. But the ability to run the application as a service is still there, and the program with the original name will help us with this. Non-Sucking Service Manager.

NSSM is free and open source software and supports all operating microsoft systemsfrom Windows 2000 to Windows 8. NSSM does not require installation, just download and unpack it. The distribution includes versions for 32- and 64-bit OS. You can take the program from the nssm.cc site, at the moment the latest stable version is 2.21.1, which I will use.
To demonstrate the capabilities of NSSM, let's try to run Windows Notepad as a service on Windows 8.1.

Service creation

To create a service named notepad start the command console, go to the folder with the unpacked NSSM (for 64-bit Windows) and enter the command

The code:

Nssm install notepad

which opens the graphical NSSM installer window. To create a service, just specify the path to the executable file in the Path field and click the "Install service" button. Additionally, in the Options field, you can specify the keys required to start the service.

Also at the stage of creating a new service, you can specify some additional parameters.

The Shutdown tab lists the shutdown methods and timeouts that are used when the application is shutting down or crashing normally. When NSSM receives a stop command (for example, when an application is shutting down), it tries to stop the monitored application in a regular way. If the application does not respond, then NSSM can forcefully terminate all processes and subprocesses of this application.

There are four stages of application shutdown in total, and by default they will be used in this order:

In the first step, NSSM tries to generate and send a Ctrl + C event. This method works well for console applications or scripts, but not for graphical applications;
The NSSM then identifies all windows created by the application and sends them a WM_CLOSE message to exit the application;
As a third step, NSSM calculates all threads created by the application and sends them a WM_QUIT message, which will be received if the application has a thread's message queue;
And as a last resort, NSSM can call TerminateProcess () to force the application to shut down.

It is possible to disable some or even all of the methods, however, different methods work for different applications and it is recommended to leave everything as it is in order to properly shut down the application.

By default, when the NSSM service crashes, it tries to restart it. On the "Exit actions" tab, you can change the automatic action in case of an abnormal application shutdown, as well as set a delay before the automatic restart of the application.

On the "Input / Output (I / O)" tab, you can set the redirection of application input / output to the specified file.

On the "Environment" tab, you can set new environment variables for the service, or override existing ones.

You can also not use the graphical shell and immediately create a service in the console with the following command:

The code:

Nssm install notepad "C: \\ Windows \\ system32 \\ notepad.exe"

Service management

After creating the service using NSSM, go into the Services snap-in and find the notepad service. As you can see, it looks no different from other services, we can also start it, stop it or change the startup mode. Note, however, that nssm.exe is listed as the executable file.

And if we go into the Task Manager, we will see the following picture: NSSM is launched as the main (parent) process, the notepad service is launched as its child process, and the Notepad application is already running in this child process.

Last updated: 31.10.2015

Services are one of the most important components of the Windows operating system. In fact, these are separate applications that do not have a graphical interface and that perform various tasks in the background. Services can be started at the start of the operating system, or at any other moment of the user's work. A common example of a service is various web servers that listen on a specific port in the background for connections and, if there are connections, communicate with them. It can also be various auxiliary update services for others installed programsthat contact the server to see if there is a new version applications. In general, we can open the services panel and see for ourselves all installed and running services:

Let's look at how to create your own services in C #. As a task to be implemented, we will choose monitoring changes in a specific folder in the file system. Now let's create a service to execute it.

First, let's create new projectwhich will be of type Windows Service. Let's call the project FileWatcherService:

Visual Studio then generates a project that has everything you need. While in principle we don't have to choose this particular project type, we could create a class library project and then define all the needed classes in it.

So the new project looks like this:

There is also a Program.cs file and the actual Service1.cs service host.

The service is a normal application, but it does not start by itself. All calls and calls to it go through the Service Control Manager (SCM). When the service is started automatically at system startup or manually, the SCM calls the Main method in the Program class:

Static class Program (static void Main () (ServiceBase ServicesToRun; ServicesToRun \u003d new ServiceBase (new Service1 ()); ServiceBase.Run (ServicesToRun);))

The Main method is defined by default to start multiple services at once, which are defined in the ServicesToRun array. However, by default, the project contains only one service, Service1. The launch itself is performed using the Run: ServiceBase.Run (ServicesToRun) method.

The service itself being started is represented by the Service1.cs node. However, this is not really a simple code file. If we open this node, we will see the service designer file Service1.Designer.cs and the Service1 class.

The Service1 class itself represents the service. By default, it has the following code:

Using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading.Tasks; namespace FileWatcherService (public partial class Service1: ServiceBase (public Service1 () (InitializeComponent ();) protected override void OnStart (string args) () protected override void OnStop () ()))

The service class must inherit from the base ServiceBase class. This class defines a number of methods, the most important of which are the OnStart () method, which starts the actions executed by the service, and the OnStop () method, which stops the service.

After the SCM calls the Main method and registers the service, it is called directly by running the OnStart method.

When in the services console or via command line we send a command to stop the service, then the SCM calls the OnStop method to stop it.

In addition to these two methods in the service class, you can override several more methods of the base ServiceBase class:

    OnPause: Called when the service is suspended

    OnContinue: Called when the service is resumed after being suspended

    OnShutdown: Called when Windows shuts down

    OnPowerEvent: Called when the power mode changes

    OnCustomCommand: Called when received by the service custom command from Service Control Manager (SCM)

In the constructor of the Service1 class, the InitializeComponent () method is called, which is defined in the designer file Service1.Designer.cs:

Namespace FileWatcherService (partial class Service1 (private System.ComponentModel.IContainer components \u003d null; protected override void Dispose (bool disposing) (if (disposing && (components! \u003d Null)) (components.Dispose ();) base.Dispose (disposing );) private void InitializeComponent () (components \u003d new System.ComponentModel.Container (); this.ServiceName \u003d "Service1";)))

The only thing to note in it is setting the name of the service (the ServiceName property):

This.ServiceName \u003d "Service1";

This is the name that will be displayed in the services console after installing this service. We can change it, or we can leave it as it is.

Now let's change the service code as follows:

Using System; using System.ServiceProcess; using System.IO; using System.Threading; namespace FileWatcherService (public partial class Service1: ServiceBase (Logger logger; public Service1 () (InitializeComponent (); this.CanStop \u003d true; this.CanPauseAndContinue \u003d true; this.AutoLog \u003d true;) protected override void OnStart (string args) ( logger \u003d new Logger (); Thread loggerThread \u003d new Thread (new ThreadStart (logger.Start)); loggerThread.Start ();) protected override void OnStop () (logger.Stop (); Thread.Sleep (1000);) ) class Logger (FileSystemWatcher watcher; object obj \u003d new object (); bool enabled \u003d true; public Logger () (watcher \u003d new FileSystemWatcher ("D: \\\\ Temp"); watcher.Deleted + \u003d Watcher_Deleted; watcher.Created + \u003d Watcher_Created; watcher.Changed + \u003d Watcher_Changed; watcher.Renamed + \u003d Watcher_Renamed;) public void Start () (watcher.EnableRaisingEvents \u003d true; while (enabled) (Thread.Sleep (1000);)) public void Stop () ( watcher.EnableRaisingEvents \u003d false; enabled \u003d false;) // rename privat files e void Watcher_Renamed (object sender, RenamedEventArgs e) (string fileEvent \u003d "renamed to" + e.FullPath; string filePath \u003d e.OldFullPath; RecordEntry (fileEvent, filePath); ) // changing files private void Watcher_Changed (object sender, FileSystemEventArgs e) (string fileEvent \u003d "changed"; string filePath \u003d e.FullPath; RecordEntry (fileEvent, filePath);) // creating files private void Watcher_Created (object sender, FileSystemEventArgs e) (string fileEvent \u003d "created"; string filePath \u003d e.FullPath; RecordEntry (fileEvent, filePath);) // delete files private void Watcher_Deleted (object sender, FileSystemEventArgs e) (string fileEvent \u003d "deleted"; string filePath \u003d e.FullPath; RecordEntry (fileEvent, filePath);) private void RecordEntry (string fileEvent, string filePath) (lock (obj) (using (StreamWriter writer \u003d new StreamWriter ("D: \\\\ templog.txt", true)) ( writer.WriteLine (String.Format ("(0) file (1) was (2)", DateTime.Now.ToString ("dd / MM / yyyy hh: mm: ss"), filePath, fileEvent)); writer. Flush ();)))))

The key class that encapsulates all the functionality is the Logger class. Using the FileSystemWatcher object, it will monitor changes in the folder D: // Temp... In the Start () method, it is set that we will track changes through the FileSystemWatcher object. And all work will go as long as the enabled boolean variable is true. And the Stop () method will allow the class to terminate.

FileSystemWatcher events track all changes to the watched folder. This will log the changes to the templog.txt file. To avoid a resource race for the templog.txt file that records changes, the write routine is locked with a lock (obj) stub.

As a result, after creating, changing, renaming and deleting the log file will contain something like:

2015-07-30 12:15:40 PM file D: \\ Temp \\ New text document.txt was created on 07/30/2015 12:15:46 PM D: \\ Temp \\ New text document.txt file was renamed to D: \\ Temp \\ hello. txt 07/30/2015 12:15:55 PM file D: \\ Temp \\ hello.txt was changed on 07/30/2015 12:15:55 PM D: \\ Temp \\ hello.txt file was modified on 07/30/2015 12:16:01 PM file D: \\ Temp \\ hello.txt has been removed

In the service class Service1 itself, a number of options are set in the constructor:

This.CanStop \u003d true; // the service can be stopped this.CanPauseAndContinue \u003d true; // the service can be paused and then resumed this.AutoLog \u003d true; // service can write to the log

In the OnStart () method, a new thread is called to start the Logger object:

Protected override void OnStart (string args) (logger \u003d new Logger (); Thread loggerThread \u003d new Thread (new ThreadStart (logger.Start)); loggerThread.Start ();)

The new thread is needed because the current thread only processes SCM commands and should return from the OnStart method as quickly as possible.

When a command is received from the SCM to stop the service, the OnStop method fires, which calls the logger.Stop () method. Additional delay will allow the logger thread to stop:

Protected override void OnStop () (logger.Stop (); Thread.Sleep (1000);)

However, the service class itself is not enough yet. We also need to create a service installer.

In terms of software compatibility. So it's only natural that we come back to discussing services in the context of Windows 7. But this time we'll talk about some of the benefits of service optimization available in Windows 7. This article focuses on new opportunity Windows 7 - Trigger Start Services... But before we get into the API, let's outline the big picture of services.

What are services?

Service is an internal mechanism built into the operating room windows system... You can think of services as special applications that run regardless of the current user context. A service differs from a regular application in that it can be configured to run from the time the system is turned on (boot) to shutdown, without requiring the user to be present. That is, the services can run even if the user is not logged in.

We prefer to think of services as running tasks, running in the background and not affecting user operations. Services on Windows are responsible for all kinds of background activity, from Remote Procedure Call (RPC), Printer Spooler to Network Location Awareness.

Over the years, Windows has grown and at the same time the number of services has increased. Let's be honest, background services on Windows feel pretty painful - operating system comes with many services initially. In addition, independent software developers (ISVs) and their applications add even more services. For example, software update services. However, some services are critical and required during the boot process, while others are needed later when a certain user logs in, and others do not need to be started at all until they are called. Regardless, when you look at the list of currently running services, you see a lot of objects that do not need to operate 24x7.

What's wrong with a service that runs 24 hours a day, 7 days a week?

There are several problems with 24x7 services. First, why should something work (even in the background) if there is no need for it? Any running process (including services) uses up precious memory and CPU resources that could be used for other applications and services. If you count all the services running at a given moment, they add up to a significant amount of memory, descriptors, threads, and CPU usage. All of these "wasted" resources reduce the overall performance of the computer, its responsiveness, and make it appear sluggish and slow. In addition, since many services are configured to automatic start (start working at system startup), they affect the computer boot time.

Second, these wasted resources have a direct impact on energy consumption. The greater the load on the CPU, the more power the computer consumes. This can be critical for laptops and can reduce battery life by several hours.

Thirdly, the constant operation of unproductive software can lead to memory leaks and general system instability. This leads to the failure of applications and, ultimately, the computer.

Finally, if a service is running 24x7, and if it is a well-known service (which every popular application might have, such as PDF Reader), then this creates a large surface for attack. An attacker could take advantage of the knowledge that a particular popular application is installing a 24x7 service and try to hack it to gain access to the computer.

With all of the above, you might wonder why so many developers set up their services to run all the time when they have another option. Even before Windows 7, there were several options available for starting services:

  • Disabled disables the service completely and prevents it and dependent services from starting - this means that the user has to enable the service manually from the control panel or command line
  • Manual starts a service as needed (due to dependencies of other services) or when calling a service from an application using the appropriate API, as will be shown below
  • Automatic starts the service at login
  • Automatic Delayed - A newer startup type introduced in Windows Vista, with which the service starts after boot completion and initial operations, which speeds up system startup.

Unfortunately, many ISVs (including Microsoft itself) continue to configure their services to be Automated or Automatic Delayed, since everyone seems to be the simplest solution. The service just runs 24x7 and is always available, eliminating any need to check dependencies or whether the service is running.

There are many examples of existing services that can use less resources and become safer without running 24x7. For example, think of an update service that checks for new updates to an app. If the computer is not connected to a network and does not have an IP address, why would it work? It can't do anything, so why leave a program running that doesn't do anything? Think about the policy management service used when changing group policies or when the computer joins or disconnects from the domain, but now that the computer is connected to my home network, the service is again wasted.

Emergence of Triggered Services

The solution to the above problems is to move the service out of a "running state" into other kinds of background activity, such as scheduled tasks or triggered services. This article is about Windows 7 Trigger Start Services. A lot of interesting things can be said about Windows 7 Scheduled Tasks, which will be done in subsequent articles.