首页 > 解决方案 > 从 C# 运行 tabcmd 命令时 Process.Start() 未完成

问题描述

我有一个任务,我必须从 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#cmdtableau-apiprocess.start

解决方案


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

        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 事件。
要获取更多信息,请参阅此链接


推荐阅读