Why is Process.Start returning false unexpectedly

When using Process.Startfor a shell of another executable (not the file type), why should it return false and not run the executable rather than throwing an exception? To be more clear, the documentation mentions that return is "true if a process resource is started, false if a new process resource is not started (for example, if an existing process is reused)". All explanations for "existing process are reused" seem to be for launching files that open in an existing instance of the program that processes them (for example, launches a .jpg that opens in an existing instance of an image editor). The released executable in this context is proprietary and does nothing to prevent new instances from being executed. There are no records or other signswhich the application even tried to launch. Feedback for why the process was not started has not been found anywhere.

It should be noted that the problem becomes more reproducible, the higher the number of executable files launched in quick succession. The same code doesn't seem to work in production environments, but the load is better distributed across many machines. In a test environment, more than 100 processes start quickly on a single machine, and pretty consistently the last 7-10 are the only ones not running. The management process is capable of detecting already running processes when it starts, and only starts those that are not currently running. It seems to work correctly and when we do a full fresh start (all 100+ instances are checked to be stopped and then we start them) 7 or so won't start.but then we can restart the control process and the same executables with the same settings are executed successfully. It can be helpful to know that all running processes have the same executable with different command line arguments. The control process is a Windows service, the generated executables are command line applications that do not depend on standard I / O streams.

In the process of troubleshooting this issue, to be on the safe side, I ensured that the thread that launches external applications would run in a single threaded apartment because ShellExecuteEx might depend on this, although I believe it is a framework now. I tried to add Thread.Sleep delays between processes, which doesn't seem to affect the number of applications that fail to start (with various delays taken from 100ms to 300ms, and more recently after each instance reaching 1500 ms or so).

Perhaps there is a maximum number of child processes overlaid by windows, or perhaps some kind of error that occurs when Windows tries to start many processes at the same time? I have not been able to find a reasonable answer to the question of why the execution fails. Here is the bit that launches the executable itself (some internal changes have been edited, but all the framework code is intact):

        if (!Path.IsPathRooted(processPath)) 
        {
            processPath = Path.GetFullPath(Path.Combine(
                Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
                processPath));
        }

        ProcessStartInfo psi =
                new ProcessStartInfo(processPath, args);
        psi.UseShellExecute = true;
        psi.CreateNoWindow = true;

        Trace("Starting {0} {1}", processPath, args);

        using (Process process = new Process())
        {
            process.StartInfo = psi;
            bool success = process.Start();
            if (!success)
            {
                // this is what doesn't make sense
            }
        }

        Trace("Started {0} {1}", processPath, args);

      

+3


source to share


1 answer


I'm a little surprised that it didn't throw Win32Exception. Try to retrieve the latest Win32 error after Process.Start () failed. You do it with:

var errorCode = Marshal.GetLastWin32Error();

      



Then, assuming you get a Win32 error, take a look at this site:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms681381(v=vs.85).aspx

0


source







All Articles