It seems, that Windows Services cannot create more than a certain number of processes. In the attached Visual Studio Solution, a Windows Service starts 100 processes. However, after a certain number of processes created, the subsequent processes fail to start. Using the same code in a Console Application, all processes start successfully. The number of failed processes seems to vary between computers. On a computer with Windows 7, there were 15 (out of 100) failed processes, on a computer with Windows Server 2012 there were 4 (out of 100) failed processes. Therefore, the following questions arise:
- Is there a configuration that limits the number of processes created by a Windows Service?
- If this is a bug, is there a patch for this?
- Which is the recommended way to start processes from a Windows Service?
Steps to reproduce the problem are listed below:
- Unzip WinServiceCreateProcesses.zip.
- Open the created folder, WinServiceCreateProcesses.
- Open WinServiceCreateProcesses.sln. The solution contains two projects:
WindowsService1
and ConsoleApplication1
. WindowsService1
is the Windows Service that starts 100 processes of ConsoleApplication1
. Compile the solution. - Double click on InstallService.bat. This will install the Windows Service
WindowsService1
.
Depending on the user’s privileges, this might have to be run as administrator. - Start/Run/Services.msc to start the Services Console.
- Select
WindowsService1
, right-click, select Start to start the Windows Service. - Start/Run/Eventvwr.msc to start the Event Viewer.
- Select Windows Logs/Application, right-click, select Refresh.
- The latest log entry should be similar to this:
StartService nr alive=85, nr dead=15,
dead ids=86,87,88,89,90,91,92,93,94,95,96,97,98,99,100
That means, that 100 processes were started, but 15 seem not to be alive.
This may be verified, by counting the number of ConsoleApplication1
processes in the Task Manager.
- In the Services Console, select
WindowsService1
, right-click, select Stop to stop the Windows Service.
This will kill all the started ConsoleApplication1
processes. If something went wrong, these may be killed by double-clicking on KillProcesses.bat. Depending on the user’s privileges, this might have to be run as administrator. - In Visual Studio/Solution Explorer, right-click on
WindowsService1
and select Properties. - Change the Application Type to Console Application and change the
Startup
object to Module1
. This way,
WindowsService1
is transformed to a Console Application. - Recompile the solution.
- Double-click on bin/WindowsService1.exe.
This will start WindowsService1.exe as a Console Application, which in turn will open 100 Console windows. - In the Event Viewer/Windows Logs/Application, right-click, select Refresh.
- The latest log entry should be similar to this:
StartService nr alive=100, nr dead=0, dead ids=
That means, that 100 processes were started, and all 100 seem to be alive.
This may be verified, by counting the number of ConsoleApplication1
processes in the Task Manager.
- Type the Enter key in the first Console Window (which is the WindowsService1.exe window) to stop the
Console Application WindowsService1.exe. This will kill all the started ConsoleApplication1
processes. - Double click on UnInstallService.bat. This will uninstall the Windows Service
WindowsService1
.
History
- 20th January, 2016: Initial version