c# - 从 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"
解决方案
修改您的代码如下并再次测试:
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 事件。
要获取更多信息,请参阅此链接
推荐阅读
- go - Go 需要逗号,当放在那里时,会抛出其他不相关的错误
- testing - 如何在 .Net Core Web 应用程序的每次运行中保持一致的 RequestVerificationToken
- html - 为什么垂直对齐在这个特定示例中不起作用
- swift - 使用未声明的类型 - Swift - AWS Amplify
- c++ - 如何在 Xcode 10 for C++ 项目中查看测试的代码覆盖率
- html - 要求用户至少选择一个 HTML 复选框
- java - 我如何使用 if 语句来知道是玩家赢了还是电脑赢了?
- reactjs - React Material UI:如何使用material-ui白金主题?
- javascript - 分为html页面、javascript页面和css页面
- c# - 如何从 Gridview 上的存储过程而不是“System.Data.DataSet”中获取实际值?