在winform进度条中显示运行脚本的进度

发布于 2024-11-27 01:17:52 字数 267 浏览 2 评论 0原文

我有以下代码:

Process scriptProc = new Process();
scriptProc.StartInfo.FileName = @"cscript";
scriptProc.Start();
scriptProc.WaitForExit();
scriptProc.Close();

我想隐藏执行上述代码时将显示的 cscript 窗口。有什么方法可以在 winform 进度条控件中显示上述脚本进度吗?

谢谢。

I have the following code:

Process scriptProc = new Process();
scriptProc.StartInfo.FileName = @"cscript";
scriptProc.Start();
scriptProc.WaitForExit();
scriptProc.Close();

And I want to hide that cscript window that will show when I execute the above code. And is there any way I can show the above script progress in a winform progressbar control?

Thanks.

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

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

发布评论

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

评论(1

梦亿 2024-12-04 01:17:52

要启动进程而不显示新窗口,请尝试:

    scriptProc.StartInfo.CreateNoWindow = true;

要显示脚本进度,您需要脚本将其进度文本写入 stdout,然后让调用程序读取进度文本并将其显示在用户界面中。像这样的东西:

   using ( var proc = new Process() )
    {
        proc.StartInfo = new ProcessStartInfo( "cscript" );
        proc.StartInfo.CreateNoWindow = true;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StartInfo.UseShellExecute = false;

        proc.OutputDataReceived += new DataReceivedEventHandler( proc_OutputDataReceived );
        proc.Start();
        proc.BeginOutputReadLine();
        proc.WaitForExit();
        proc.OutputDataReceived -= new DataReceivedEventHandler( proc_OutputDataReceived );

    }

void proc_OutputDataReceived( object sender, DataReceivedEventArgs e )
{
    var line = e.Data;

    if ( !String.IsNullOrEmpty( line ) )
    {
        //TODO: at this point, the variable "line" contains the progress
        // text from your script. So you can do whatever you want with
        // this text, such as displaying it in a label control on your form, or
        // convert the text to an integer that represents a percentage complete
        // and set the progress bar value to that number.

    }
}

To start a process without showing a new window, try:

    scriptProc.StartInfo.CreateNoWindow = true;

To show script progress, you need the script to write its progress text to stdout, then have the calling program read the progress text and display it in your user interface. Something like this:

   using ( var proc = new Process() )
    {
        proc.StartInfo = new ProcessStartInfo( "cscript" );
        proc.StartInfo.CreateNoWindow = true;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StartInfo.UseShellExecute = false;

        proc.OutputDataReceived += new DataReceivedEventHandler( proc_OutputDataReceived );
        proc.Start();
        proc.BeginOutputReadLine();
        proc.WaitForExit();
        proc.OutputDataReceived -= new DataReceivedEventHandler( proc_OutputDataReceived );

    }

void proc_OutputDataReceived( object sender, DataReceivedEventArgs e )
{
    var line = e.Data;

    if ( !String.IsNullOrEmpty( line ) )
    {
        //TODO: at this point, the variable "line" contains the progress
        // text from your script. So you can do whatever you want with
        // this text, such as displaying it in a label control on your form, or
        // convert the text to an integer that represents a percentage complete
        // and set the progress bar value to that number.

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