show stderr output dynamically

A

Andrus

Long-running backup program mybackup.exe wrotes status messages to stderr
How to show messages written to stderr if this program is running ?

I tried code below but it shows stderr output only after program is
completed.

Andrus.

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;

class DBCreate
{
internal string msg;

internal void dbCreate()
{
ProcessStartInfo pinfo = new ProcessStartInfo();
pinfo.ErrorDialog = true;
pinfo.FileName = Environment.CurrentDirectory +
"\\mybackup.exe";
pinfo.WindowStyle = ProcessWindowStyle.Maximized;
pinfo.RedirectStandardError = true;
pinfo.UseShellExecute = false;
pinfo.RedirectStandardOutput = true;
Process p = new Process();
p.EnableRaisingEvents = true;
p.StartInfo = pinfo;
p.Start();
msg = p.StandardError.ReadToEnd();

while (!p.HasExited)
System.Threading.Thread.Sleep(1000);

p.WaitForExit();
p.Close();
}
}

public static class BackupCopy
{
public static void Run()
{
var bw = new BackupbackgroundWorker();
bw.RunWorkerAsync();
}
}

class BackupbackgroundWorker : BackgroundWorker
{
string msg;
DBCreate db;

protected override void OnDoWork(DoWorkEventArgs e)
{
db = new DBCreate();
db.dbCreate();
msg = db.msg;
base.OnDoWork(e);
}

protected override void
OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
{
base.OnRunWorkerCompleted(e);
MessageBox.Show( msg );
}
}
 
B

Ben Voigt [C++ MVP]

-- For a process that has already exited, calling Process.Close()
is superfluous.

False. Windows has to keep a bunch of data structures around for every
process, even after exit, until every last handle to it is closed. This is
so that other programs can inspect the exit code and wait for the process to
exit. Ok, .NET's garbage collector would close that handle eventually when
it decided you weren't ever going to use it again, but better to be
deterministic.
 
B

Ben Voigt [C++ MVP]

Ben Voigt said:
False. Windows has to keep a bunch of data structures around for every
process, even after exit, until every last handle to it is closed. This
is so that other programs can inspect the exit code and wait for the
process to exit. Ok, .NET's garbage collector would close that handle
eventually when it decided you weren't ever going to use it again, but
better to be deterministic.

Also these data structures assigned to exited processes because some other
process may still be using them are called "zombie processes" in many other
OSes.
 
Top