【问题标题】:Process.Start() doesn't finish when Running tabcmd commands from C#从 C# 运行 tabcmd 命令时 Process.Start() 未完成
【发布时间】:2018-06-01 14:55:51
【问题描述】:

我有一个任务,我必须从 c# 项目运行 tabcmd 命令,该命令将从服务器读取画面报告并保存到 pdf 文件中。我正在使用以下代码来执行此操作。

string reportPath = "/Agency/AYR_CurrentYearAgencyIATA=0125452&:refresh=yes";
string currentYearPdf = @"C:\Reports\StatusReport_CurrentYear_0125452.PDF";
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.WorkingDirectory = @"C:\tabcmd\Command Line Utility\";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = false;
process.StartInfo.RedirectStandardOutput = false;
process.StartInfo.RedirectStandardError = false;
process.StartInfo.Arguments = "/C tabcmd login -s http://prodtableau -u xxx -p xxx";
process.Start();
process.StartInfo.Arguments = "'/C tabcmd export \"" + reportPath + "\"" +
    " --pdf --pagelayout landscape--pagesize legal --width 1600 -f \"" + currentYearPdf +
     "\"'";
process.Start();

当我直接从命令提示符运行这些 tabcmd 命令时,它们执行得很好,并且 pdf 文件保存到我的本地目录中,但是当通过 c# 代码运行时,第二个进程开始但它永远不会结束并且不会生成所需的 pdf 文件。 使用的 tabcmd 命令是

tabcmd export "/Agency/AYR_CurrentYearAgencyIATA=0125452&:refresh=yes" --pdf --pagelayout Landscape --pagesize legal --width 1600 -f "C:\Reports\StatusReport_CurrentYear_0125452.PDF"

【问题讨论】:

    标签: c# cmd tableau-api process.start


    【解决方案1】:

    如下修改你的代码并再次测试:

            process.StartInfo.Arguments = "/C tabcmd login -s http://prodtableau -u xxx -p xxx";
            process.Start();
    
            process.WaitForExit()
    
            process.StartInfo.Arguments = "'/C tabcmd export \"" + @reportPath + "\"" + " --pdf --pagelayout landscape--pagesize legal --width 1600 -f \"" + @currentYearPdf + "\"'";
            process.Start();
    
            process.WaitForExit()
    

    WaitForExit() 使当前线程等待,直到相关进程终止。应该在进程上调用所有其他方法之后调用它。为避免阻塞当前线程,请使用 Exited 事件。
    如需了解更多信息,请参阅link

    【讨论】:

    • @venky 你可以创建一个新进程。你为什么不这样做?
    • 这两种方式都不起作用。您能否检查我的 process.Startinfo.Arguments 是否与以下命令匹配 >tabcmd export "/Agency/AYR_CurrentYearAgencyIATA=0125452&:refresh=yes" --pdf --pagelayout Landscape --pagesize legal --width 1600 -f "C: \Reports\StatusReport_CurrentYear_0125452.PDF"
    • @venky 似乎是真的。也许引号是问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    • 1970-01-01
    • 2020-09-05
    • 2011-09-20
    相关资源
    最近更新 更多