0

I am executing a commandline via Process. When executed, the whole os kind of freezed, and it does the process does not terminate. However, if I execute my command manually, it works fine and terminates quick.

Here is the whole method, which I am using. Could it be, that there is a mistake in the way I am starting the process?

public int ExecViaCmdLine(String dtExecPath, String tmpDir, Package pkg, String pkgName, bool optionShowCmdBox, bool optionTidyUpTmpDir) {
        Console.WriteLine("Execute via Commandline DTEXEC: " + pkgName);
        pkgName = pkgName.Replace(" ", "");

        String tmpPackagePath = SaveToTempFile(pkg, pkgName, tmpDir);
        String tmpPackageName = Path.GetFileName(tmpPackagePath);


        String arguments = "/C \"" + dtExecPath + "\" /f " + tmpPackageName;
        Console.WriteLine("Working Dir: " + tmpDir);
        Console.WriteLine(arguments);
        //


        System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        if (optionShowCmdBox)
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
        else
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        startInfo.FileName = "cmd.exe";
        startInfo.WorkingDirectory = tmpDir;
        startInfo.Arguments = arguments;
        startInfo.RedirectStandardError = true;
        startInfo.RedirectStandardOutput = true;
        startInfo.UseShellExecute = false;
        process.StartInfo = startInfo;
        process.Start();

        // To avoid deadlocks, always read the output stream first and then wait.  
        string errOutput = process.StandardError.ReadToEnd();
        string stdOutput = process.StandardOutput.ReadToEnd();
        process.WaitForExit();
        //Console.WriteLine($"\nError stream: {errOutput}");
        //Console.WriteLine($"\nStandard stream: {stdOutput}");

        // Tidy up
        if (optionTidyUpTmpDir)
        {
            File.Delete(tmpPackagePath);
        }

        return process.ExitCode;
    }

When I execute the command, which I created, manualy, it works fine within a one second.

    c:\tmp>"C:\Program Files (x86)\Microsoft SQL Server\130\DTS\Binn\DTEXEC.exe" /f 20200511041318780ImpTextTemplate
Microsoft (R) SQL Server Execute Package Utility
Version 13.0.4561.14 for 32-bit
Copyright (C) 2016 Microsoft. All rights reserved.

Started:  16:14:05
Progress: 2020-05-11 16:14:06.40
   Source: Preparation SQL Task 1
   Executing query "IF object_id('[dbo].[STG_-16]') IS NOT NULL DROP T...".: 50% complete
End Progress
Progress: 2020-05-11 16:14:06.40
   Source: Preparation SQL Task 1
   Executing query "
CREATE TABLE [dbo].[STG_-16] (
   [GRUPPE1]  NVAR...".: 100% complete
End Progress
Progress: 2020-05-11 16:14:06.41
   Source: Data Flow Task 1
   Validating: 0% complete
End Progress
Progress: 2020-05-11 16:14:06.42
   Source: Data Flow Task 1
   Validating: 50% complete
End Progress
Warning: 2020-05-11 16:14:06.42
   Code: 0x800470C8
   Source: Data Flow Task 1 Flatfilequelle [142]
   Description: The external columns for Flatfilequelle are out of synchronization with the data source columns. The external column "GRUPPE1" needs to be updated.
The external column "GRUPPE2" needs to be updated.
The external column "GRUPPE3" needs to be updated.
The external column "HERSTELLER" needs to be updated.
The external column "ARTIKEL_NR" needs to be updated.
The external column "HSTNUMMER" needs to be updated.
The external column "ARTIKEL1" needs to be updated.
The external column "ARTIKEL2" needs to be updated.
The external column "FVERSION" needs to be updated.
The external column "FSYSTEM" needs to be updated.
The external column "FSPRACHE" needs to be updated.
The external column "FFORMAT" needs to be updated.
The external column "FBUS" needs to be updated.
The external column "FGARANTIE" needs to be updated.
The external column "EVP" needs to be updated.
The external column "EK" needs to be updated.
The external column "AKTIONPR" needs to be updated.
The external column "AKTBIS" needs to be updated.
The external column "AKTION" needs to be updated.
The external column "VERFUEGBARKZ" needs to be updated.
The external column "VMENGE" needs to be updated.
The external column "VDATUM" needs to be updated.
The external column "VETA" needs to be updated.
The external column "NACHFOLGER" needs to be updated.
The external column "DATENBLATT1" needs to be updated.
The external column "BILD1" needs to be updated.
The external column "ERFDAT" needs to be updated.
The external column "EANCODE" needs to be updated.
The external column "UPCCODE" needs to be updated.
The external column "HOEHE" needs to be updated.
The external column "BREITE" needs to be updated.
The external column "LAENGE" needs to be updated.
The external column "GEWICHT" needs to be updated.
The external column "RANG" needs to be updated.
The external column "STCK_KART" needs to be updated.
The external column "STCK_PAL" needs to be updated.
The external column "STORNO" needs to be updated.
The external column "ECLASS" needs to be updated.
The external column "UNSPC" needs to be updated.
The external column "INTRASTAT" needs to be updated.
The external column "CATALOGID" needs to be updated.
End Warning
Progress: 2020-05-11 16:14:06.42
   Source: Data Flow Task 1
   Validating: 100% complete
End Progress
Warning: 2020-05-11 16:14:06.42
   Code: 0x80049304
   Source: Data Flow Task 1 SSIS.Pipeline
   Description: Warning: Could not open global shared memory to communicate with performance DLL; data flow performance counters are not available.  To resolve, run this package as an administrator, or on the system's console.
End Warning
Progress: 2020-05-11 16:14:06.43
   Source: Data Flow Task 1
   Prepare for Execute: 0% complete
End Progress
Progress: 2020-05-11 16:14:06.43
   Source: Data Flow Task 1
   Prepare for Execute: 50% complete
End Progress
Progress: 2020-05-11 16:14:06.43
   Source: Data Flow Task 1
   Prepare for Execute: 100% complete
End Progress
Progress: 2020-05-11 16:14:06.44
   Source: Data Flow Task 1
   Pre-Execute: 0% complete
End Progress
Progress: 2020-05-11 16:14:06.47
   Source: Data Flow Task 1
   Pre-Execute: 50% complete
End Progress
Progress: 2020-05-11 16:14:06.50
   Source: Data Flow Task 1
   Pre-Execute: 100% complete
End Progress
Progress: 2020-05-11 16:14:06.74
   Source: Data Flow Task 1
   Post Execute: 0% complete
End Progress
Progress: 2020-05-11 16:14:06.74
   Source: Data Flow Task 1
   Post Execute: 50% complete
End Progress
Progress: 2020-05-11 16:14:06.74
   Source: Data Flow Task 1
   Post Execute: 100% complete
End Progress
Progress: 2020-05-11 16:14:06.74
   Source: Data Flow Task 1
   Cleanup: 0% complete
End Progress
Progress: 2020-05-11 16:14:06.74
   Source: Data Flow Task 1
   Cleanup: 50% complete
End Progress
Progress: 2020-05-11 16:14:06.74
   Source: Data Flow Task 1
   Cleanup: 100% complete
End Progress
DTExec: The package execution returned DTSER_SUCCESS (0).
Started:  16:14:05
Finished: 16:14:06
Elapsed:  0.797 seconds
tobi
  • 753
  • 1
  • 14
  • 25
  • could it because of permission of the exe? – urlreader May 11 '20 at 14:33
  • 1
    Did you try to read an output asynchronously? Because you have a lot of text on it. There is also no need to use fully qualified class name `System.Diagnostics.Process`, you can just add `using System.Diagnostics;` at the top of file – Pavel Anikhouski May 11 '20 at 14:35
  • 3
    Why are you running `cmd.exe` instead of `DTEXEC.exe` ? – Fildor May 11 '20 at 14:53
  • Create the process you want to run, not cmd, but the problem is that you need to read the output asynchronously. – mikelegg May 11 '20 at 15:18
  • meanwhile I found out, that it just does not work in my unittests. There seems to be a problem, when called from unittests. Do you know why? – tobi May 11 '20 at 19:28

1 Answers1

1

You are running cmd.exe not DTEXEC.exe which doesn't make, apparently, much sense:

startInfo.FileName = "cmd.exe";

On top of that you are waiting for the process to exit which it never does (nothing is closing cmd.exe)

process.WaitForExit();

making you believe that the application you think is running is taking forever...

InBetween
  • 32,319
  • 3
  • 50
  • 90
  • Makes sense. But I changed it to `startInfo.FileName = dtExecPath;` and still have the same behaviour. I also tried to force errors by giving dtexec wrong parameters - no change at all. – tobi May 11 '20 at 18:11
  • Oh wait.. I executed this code in form of a unit test. In a usual program it works! – tobi May 11 '20 at 18:19