如何运行控制台应用程序、捕获输出并以文字形式显示它?

发布于 2024-10-19 01:40:16 字数 1413 浏览 7 评论 0原文

我发现我可以使用 System.Diagnostics.Process 启动进程。我正在尝试使用以下代码,但它不起作用。页面挂起,我必须重新启动 IIS...

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;

public partial class VideoTest : System.Web.UI.Page
{
    List<string> outputLines = new List<string>();
    bool exited = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        string AppPath = Request.PhysicalApplicationPath;

        Process myProcess = new Process();

        myProcess.StartInfo.UseShellExecute = false;
        myProcess.StartInfo.FileName = AppPath + "\\bin\\ffmpeg.exe";
        myProcess.StartInfo.CreateNoWindow = true;
        myProcess.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
        myProcess.Exited += new EventHandler(ExitHandler);
        myProcess.Start();

        while (!exited)
        {
            // This is bad bad bad bad....
        }

        litTest.Text = "";
        foreach (string line in outputLines)
            litTest.Text += line;
    }

    private void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
    {
        outputLines.Add(outLine.Data);
    }

    // Handle Exited event and display process information.
    private void ExitHandler(object sender, System.EventArgs e)
    {
        exited = true;
    }
}

I see that I can start processes with System.Diagnostics.Process. I'm trying with the following code, but its not working. The page just hangs and I have to restart IIS...

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;

public partial class VideoTest : System.Web.UI.Page
{
    List<string> outputLines = new List<string>();
    bool exited = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        string AppPath = Request.PhysicalApplicationPath;

        Process myProcess = new Process();

        myProcess.StartInfo.UseShellExecute = false;
        myProcess.StartInfo.FileName = AppPath + "\\bin\\ffmpeg.exe";
        myProcess.StartInfo.CreateNoWindow = true;
        myProcess.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
        myProcess.Exited += new EventHandler(ExitHandler);
        myProcess.Start();

        while (!exited)
        {
            // This is bad bad bad bad....
        }

        litTest.Text = "";
        foreach (string line in outputLines)
            litTest.Text += line;
    }

    private void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
    {
        outputLines.Add(outLine.Data);
    }

    // Handle Exited event and display process information.
    private void ExitHandler(object sender, System.EventArgs e)
    {
        exited = true;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欲拥i 2024-10-26 01:40:16

我做了一些与您的解决方案非常相似的事情——这对我来说工作得很好:

ProcessStartInfo pInfo = new ProcessStartInfo("cmd.exe");
pInfo.FileName = exePath;
pInfo.WorkingDirectory = new FileInfo(exePath).DirectoryName;
pInfo.Arguments = args;
pInfo.CreateNoWindow = false;
pInfo.UseShellExecute = false;
pInfo.WindowStyle = ProcessWindowStyle.Normal;
pInfo.RedirectStandardOutput = true;
Process p = Process.Start(pInfo);
p.OutputDataReceived += p_OutputDataReceived;
p.BeginOutputReadLine();
p.WaitForExit();
// set status based on return code.
if (p.ExitCode == 0) this.Status = StatusEnum.CompletedSuccess;
   else this.Status = StatusEnum.CompletedFailure;

有趣的区别似乎是 WaitForExit() 的使用,也可能是 BeginOutputReadLine() 的使用。

I've done something very similar to your solution -- this is working fine for me:

ProcessStartInfo pInfo = new ProcessStartInfo("cmd.exe");
pInfo.FileName = exePath;
pInfo.WorkingDirectory = new FileInfo(exePath).DirectoryName;
pInfo.Arguments = args;
pInfo.CreateNoWindow = false;
pInfo.UseShellExecute = false;
pInfo.WindowStyle = ProcessWindowStyle.Normal;
pInfo.RedirectStandardOutput = true;
Process p = Process.Start(pInfo);
p.OutputDataReceived += p_OutputDataReceived;
p.BeginOutputReadLine();
p.WaitForExit();
// set status based on return code.
if (p.ExitCode == 0) this.Status = StatusEnum.CompletedSuccess;
   else this.Status = StatusEnum.CompletedFailure;

The interesting differences seem to be the use of WaitForExit(), and possibly the BeginOutputReadLine().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文