进程似乎没有在 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 (将#修改为@)