2

It seems there are questions close to this, but none I have seen involve the actual .Net Process object. Currently, I am using a Process object to start an external executable and read data from it in C#. This happens once for each collection point that I must monitor data for. However, when I have to monitor 5 or more collection points my process for the fifth collection point is killed before I can collect any data from it. The code used to start the Process object is list below. Any help is appreciated.

    procCollectionMonitor = new Process();
    procCollectionMonitor.StartInfo.FileName = options.CollectionMonitorProcessPath;

    procCollectionMonitor.StartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(options.CollectionMonitorProcessPath);
    procCollectionMonitor.StartInfo.ErrorDialog = false;

    procCollectionMonitor.StartInfo.UseShellExecute = false;

    procCollectionMonitor.EnableRaisingEvents = true;
    procCollectionMonitor.Exited += spawn_Exited;
    procCollectionMonitor.Start();

This application is a Windows Service, that runs on Windows Server 2008 R2. As I said before, this issue only occurs when 5 or more collection points are started. Instances where 4 or less are needed have no problems.

jnich91
  • 61
  • 1
  • 4
  • 1
    Personally, I would not be using processes for this. Do you control what the process is doing? If so, use another mechanism to multiplex data. – Yann Ramin Oct 01 '12 at 20:05
  • It seems very unlikely. Have you checked whether the same thing happens if you start five copies of the external executable from the command line? Perhaps the executable itself is limiting the number of simultaneous instances. – Harry Johnston Oct 01 '12 at 21:27
  • You might want to catch any exceptions thrown by the failing 5th process, and analyze that. But to answer your question, no magic number limit. The only real limit will be available system resources. – Chris O Oct 01 '12 at 23:21
  • I did not mention this in the original post, but when I run the project in VS, it will start all 5 process with no problem. They do not run in the background but they all start and work properly. The issue only occurs when installed as a windows service and it starts the processes in the background. – jnich91 Oct 04 '12 at 14:32

2 Answers2

2

Since you are using a Windows Service, there is definitely a limit. The limit can be controlled through Desktop Heap. Check the below answer for more details on this:

How to increase the maximum number of child processes that can be spawned by a windows service -- desktop heap limits

You simply need to do the following:

Go to regEdit, and navigate to the key:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\SubSystems

Edit the key named "Windows" from:

%SystemRoot%\system32\csrss.exe ObjectDirectory=\Windows SharedSection=1024,20480,768 Windows=On SubSystemType=Windows ServerDll=basesrv,1 ServerDll=winsrv:UserServerDllInitialization,3 ServerDll=sxssrv,4 ProfileControl=Off MaxRequestThreads=16

to:

%SystemRoot%\system32\csrss.exe ObjectDirectory=\Windows SharedSection=1024,20480,2048 Windows=On SubSystemType=Windows ServerDll=basesrv,1 ServerDll=winsrv:UserServerDllInitialization,3 ServerDll=sxssrv,4 ProfileControl=Off MaxRequestThreads=16

Also this is supposed to fix the issue, I believe the optimum solution will be to find a work around to start the process independent from the Window service (which till now I don't know how)

Updated: I found a workaround to solve this issue... you can use the method "CreateProcessAsUser" for more details please check the below link:

https://www.codeproject.com/Articles/35773//Articles/35773/Subverting-Vista-UAC-in-Both-32-and-64-bit-Archite

Sufyan Jabr
  • 791
  • 4
  • 20
1

There is no practical limit to the number of processes you can start. Starting five should certainly not be a problem. Your use of C# and .NET will not have an impact. The Process class in .NET is just a managed wrapper for the Win32 CreateProcess and ShellExecuteEx APIs. The problem must lie with program that you're launching. You'll need to analyze it to figure out what's going on.

Peter Ruderman
  • 12,241
  • 1
  • 36
  • 58
  • The only issue I have with that is I do not have the source for the program being launched. It is a 3rd party application. – jnich91 Oct 04 '12 at 14:33
  • That does make things a lot tougher. If you're feeling ambitious, you can try some black box debugging using tools like Process Monitor. Otherwise, you'll have to contact the vendor for support or find away to make do with a maximum of four processes. – Peter Ruderman Oct 05 '12 at 02:35
  • @Peter this is been tested on many apps including notepad, as long as you are calling from windows service the issue will happen. – Sufyan Jabr Nov 13 '18 at 01:01
  • That depends entirely on the load on the system. I've written Windows services that have launched dozens of child processes without any issue. There is nothing magical about Windows services that alters the way processes are launched. – Peter Ruderman Nov 13 '18 at 02:09