【问题标题】:C# executed Process does not returnC#执行的进程不返回
【发布时间】:2020-05-11 14:28:22
【问题描述】:

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

【问题讨论】:

  • 会不会是因为exe的权限?
  • 您是否尝试异步读取输出?因为你上面有很多文字。也不需要使用完全限定的类名System.Diagnostics.Process,你可以在文件顶部添加using System.Diagnostics;
  • 你为什么运行cmd.exe而不是DTEXEC.exe
  • 创建你要运行的进程,不是cmd,但问题是你需要异步读取输出。
  • 同时我发现它在我的单元测试中不起作用。从单元测试调用时似乎存在问题。你知道为什么吗?

标签: c# process processstartinfo


【解决方案1】:

您正在运行 cmd.exe 而不是 DTEXEC.exe,这显然没有多大意义:

startInfo.FileName = "cmd.exe";

最重要的是,您正在等待进程退出,而它永远不会退出(没有关闭 cmd.exe)

process.WaitForExit();

让您相信您认为正在运行的应用程序需要永远...

【讨论】:

  • 有道理。但是我将其更改为startInfo.FileName = dtExecPath; 并且仍然具有相同的行为。我还试图通过给 dtexec 错误的参数来强制错误 - 根本没有改变。
  • 哦等等..我以单元测试的形式执行了这段代码。在通常的程序中它可以工作!
猜你喜欢
  • 2014-09-08
  • 2013-11-26
  • 1970-01-01
  • 1970-01-01
  • 2012-06-13
  • 1970-01-01
  • 2020-11-05
  • 2012-10-02
  • 1970-01-01
相关资源
最近更新 更多