如何解决进程似乎没有在 C# 应用程序中运行
public static async Task<ProcessResult> ExecuteShellCommand(string command,string arguments="",int timeout=1000,bool insertWait=false)
{
var result = new ProcessResult();
using (var process = new Process())
{
process.StartInfo.FileName = command;
process.StartInfo.Arguments = arguments;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNowindow = true;
var outputBuilder = new StringBuilder();
var outputCloseEvent = new taskcompletionsource<bool>();
process.OutputDataReceived += (s,e) =>
{
// The output stream has been closed i.e. the process has terminated
if (e.Data == null)
{
outputCloseEvent.SetResult(true);
}
else
{
outputBuilder.AppendLine(e.Data);
}
};
var errorBuilder = new StringBuilder();
var errorCloseEvent = new taskcompletionsource<bool>();
process.ErrorDataReceived += (s,e) =>
{
// The error stream has been closed i.e. the process has terminated
if (e.Data == null)
{
errorCloseEvent.SetResult(true);
}
else
{
errorBuilder.AppendLine(e.Data);
}
};
bool isstarted;
try
{
process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
process.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler);
isstarted = process.Start();
StreamReader reader = process.StandardOutput;
string output = reader.ReadToEnd();
result.Output = output;
}
catch (Exception error)
{
// Usually it occurs when an executable file is not found or is not executable
result.Completed = true;
result.ExitCode = -1;
result.Output = error.Message;
isstarted = false;
}
if (isstarted)
{
// Reads the output stream first and then waits because deadlocks are possible
process.BeginoutputReadLine();
process.BeginErrorReadLine();
if (insertWait)
{
await Task.Delay(150000);
}
// Creates task to wait for process exit using timeout
var waitForExit = WaitForExitAsync(process,timeout);
// Create task to wait for process exit and closing all output streams
var processtask = Task.WhenAll(waitForExit,outputCloseEvent.Task,errorCloseEvent.Task);
// Waits process completion and then checks it was not completed by timeout
if (await Task.WhenAny(Task.Delay(timeout),processtask) == processtask && waitForExit.Result)
{
result.Completed = true;
result.ExitCode = process.ExitCode;
// Adds process output if it was completed with error
if (process.ExitCode != 0)
{
result.Output = $"{outputBuilder}{errorBuilder}";
}
}
else
{
try
{
// Kill hung process
process.Kill();
}
catch
{
}
}
}
}
return result;
}
这一行调用了 ExecuteShellCommand
方法:
var result = TestHelper.ExecuteShellCommand(MessageInjectorOptions.MessageInjectorFilename,MessageInjectorOptions.MessageInjectorParameters + " " + binaryFile + " " + topic + " " + partition,300000,true);
我的日志显示这是运行的命令:
C:\Program Files\Java\jre1.8.0_281\bin\java.exe -jar C:\Users\Administrator\Downloads\test_tool\Jar\Injector\Injector-1.0.jar FILE TOPIC 2
这应该将包含在 FILE 中的消息推送到 Kafka 主题,但这些消息不会出现在该主题上,所以我假设 jar 没有运行。如果我将命令复制并粘贴到 dos 终端并运行它,我可以看到有关该主题的消息。
我的代码有什么问题可能导致 Process
无法正确运行吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。