0

I've got a problem with Process.Start() - it is returning a process PID which is indicating that the process has started correctly, but I can't see it in processes, no log nor error is recorded from the Main method. Code which is starting process:

public static int StartBackgroundProcess(string fileName, string arguments)
{
    int processId = INVALID_PROCESS_ID;
    try
    {
        using (Process p = new Process())
        {
            p.StartInfo.FileName = fileName;
            p.StartInfo.Arguments = arguments;
            p.StartInfo.UseShellExecute = false;
            p.Start();
            processId = p.Id;
        }
    }
    catch (Exception ex) 
    {
        Logger.Error(ex);
    }
    return processId;
}

NOTE: I've got the exit code:

2016-09-19 17:44:15 [23] DEBUG Process exit code: -1073741502

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1
    Are you sure that the started process has not exited yet when you look in task manager? E.g. it might be that the process exits quickly due to invalid args. – Dirk Vollmar Sep 19 '16 at 14:04
  • 3
    `Process.HasExited`, `Process.ExitCode`, `Process.ExitTime`. – Jeroen Mostert Sep 19 '16 at 14:10
  • Nope, it is not logging anything and it should. Exception also should be logged. Note: When i start it from cmd it works correctly. – Janek Zamoyski Sep 19 '16 at 14:10
  • Process exit code: -1073741502 – Janek Zamoyski Sep 19 '16 at 14:46
  • 2
    0xc0000142 (-1073741502) is STATUS_DLL_INIT_FAILED: Initialization of the dynamic link library [name] failed. The process is terminating abnormally. – wimh Sep 19 '16 at 14:51
  • 1
    Looks like an incorrectly set working directory. Set `.p.StartInfo.WorkingDirectory` to the directory where the external executable is located (or to whatever folder you use when you start the process manually). – Dirk Vollmar Sep 19 '16 at 15:04
  • Problem is not with directory, application is started. Problem probably is that there are a lot of applications running on the server. However when i start it from cmd it's not exitng with this error code. – Janek Zamoyski Sep 20 '16 at 05:43

2 Answers2

2

Try change the file name to something you have 100% its works. Something like "C:\Windows\System32\mspaint.exe".

If this start, you should review your other file.

public static int StartBackgroundProcess(string fileName, string arguments)
        {
            int processId = INVALID_PROCESS_ID;
            try
            {
                using (Process p = new Process())
                {
                    p.StartInfo.FileName = C:\Windows\System32\mspaint.exe";
                    p.StartInfo.Arguments = "";
                    p.StartInfo.UseShellExecute = false;
                    p.Start();
                    processId = p.Id;
                }
            }
            catch (Exception ex) 
            {
                Logger.Error(ex);
            }
            return processId;
        }
0

I can see that you have your Process instance in a using block, so it is disposed right after calling Start. According to documentation, the dispose of the Process releases 'all resources used by this process'. My guess was that it stops the process, but it does not.

https://msdn.microsoft.com/en-us/library/4zfy9z3c(v=vs.110).aspx

Adam B
  • 324
  • 1
  • 8