C#执行的过程不返回

我正在通过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;
    }

当我执行手动创建的命令时,一秒钟之内可以正常工作。

    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
评论