system.diagnostics.process OutputDataReceived not quite firing

I

ice9

I seem to be missing something in this straightforward example.. but i
have a vbs file that is now simplified to one line:
Wscript.Echo "test"

When i run the process, i'd like output to be returned asynchronously
since i will be running this script many times with differetn
parameters and collecting return strings.
my return "test" text actually work in synchronous mode:
process.StandardOutput.ReadToEnd()

however in the following example, ( inside a webservice method ), the
OutputDataReceived never fires:

Process p = new Process();
p.OutputDataReceived += new
DataReceivedEventHandler(p_OutputDataReceived);
p.ErrorDataReceived += new
DataReceivedEventHandler(p_ErrorDataReceived);
p.StartInfo.FileName = @"c:\windows\system32\cscript.exe";
p.StartInfo.Arguments = @"c:\test.vbs";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.Start();


p.WaitForExit();

// this works
// Process.Start(triggerPaths);
}

void p_ErrorDataReceived(object sender, DataReceivedEventArgs e) {
string s = e.Data.ToString();
}

void p_OutputDataReceived(object sender, DataReceivedEventArgs e) {
// never fires
string s = e.Data.ToString();
}
 
W

Willy Denoyette [MVP]

ice9 said:
I seem to be missing something in this straightforward example.. but i
have a vbs file that is now simplified to one line:
Wscript.Echo "test"

When i run the process, i'd like output to be returned asynchronously
since i will be running this script many times with differetn
parameters and collecting return strings.
my return "test" text actually work in synchronous mode:
process.StandardOutput.ReadToEnd()

however in the following example, ( inside a webservice method ), the
OutputDataReceived never fires:

Process p = new Process();
p.OutputDataReceived += new
DataReceivedEventHandler(p_OutputDataReceived);
p.ErrorDataReceived += new
DataReceivedEventHandler(p_ErrorDataReceived);
p.StartInfo.FileName = @"c:\windows\system32\cscript.exe";
p.StartInfo.Arguments = @"c:\test.vbs";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.Start();


p.WaitForExit();

// this works
// Process.Start(triggerPaths);
}

void p_ErrorDataReceived(object sender, DataReceivedEventArgs e) {
string s = e.Data.ToString();
}

void p_OutputDataReceived(object sender, DataReceivedEventArgs e) {
// never fires
string s = e.Data.ToString();
}



You have to start an asynchronous read of both standarderror and standardoutput before
calling WaitForExit.
Search the docs for BeginOutputReadLine etc...

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top