开发者

Showing running script progress in winform progressbar

开发者 https://www.devze.com 2023-03-23 09:16 出处:网络
I have the following code: Process scriptPr开发者_如何学Pythonoc = new Process(); scriptProc.StartInfo.FileName = @\"cscript\";

I have the following code:

Process scriptPr开发者_如何学Pythonoc = 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.


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.

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消