T
Thorsten Tarrach
Hi,
I'm trying to run a console application and interpret its output. But I also
want to force the programme to exit after 1 second if it hasn't done so.
My first try was to use WaitForExit and read the console. Unfortunately the
console buffer is limited and the programme waits for the buffer be be
consumed before producing more.
So I turned to asynchronous reading using BeginOutputReadLine. But this
seams to result sometimes in not all lines being read. This behaviour is not
deterministic at all. On the same input the programme will fail about half
the time.
The solution would be to either increase the console buffer to make
synchronous reading possible after the process exited or fix my code
I have one further question: When I call Start on the process which is a
command line application it causes to pop up a black command line window
that takes focus. Setting WindowStyle to ProcessWindowStyle.Hidden does not
seem to help.
Here is my code. It is F# I'm afraid but it the problem in somewhere in my
usage of the .net Framework's Process class:
let result = ref ""
let dataRecv (senderbj) (e : DataReceivedEventArgs) =
lock result (fun () -> result := !result ^ e.Data ^ "\r\n")
let dataRecvHandler = new DataReceivedEventHandler(dataRecv)
let timeOut = 1000
let discharge (str:string) =
let file = new
StreamWriter(Path.Combine(Application.StartupPath,"formula.out"),false,System.Text.Encoding.ASCII)
file.Write(str)
file.Close()
let pi = new
ProcessStartInfo(Path.Combine(Application.StartupPath,"eprover"),"--tstp-format
--cpu-limit=2 -xAuto -tAuto " ^
Path.Combine(Application.StartupPath,"formula.out"))
pi.RedirectStandardOutput <- true
pi.RedirectStandardError <- true
pi.UseShellExecute <- false
pi.WindowStyle <- ProcessWindowStyle.Hidden
let p = Process.Start(pi)
lock result (fun () -> result := "")
p.OutputDataReceived.AddHandler dataRecvHandler
p.BeginOutputReadLine()
if not(p.WaitForExit(timeOut)) then
p.Kill()
p.CancelOutputRead()
p.OutputDataReceived.RemoveHandler dataRecvHandler
p.Close()
else
p.CancelOutputRead()
p.OutputDataReceived.RemoveHandler dataRecvHandler
lock result (fun () -> result := p.StandardError.ReadToEnd() ^
!result)
p.Close()
I'm trying to run a console application and interpret its output. But I also
want to force the programme to exit after 1 second if it hasn't done so.
My first try was to use WaitForExit and read the console. Unfortunately the
console buffer is limited and the programme waits for the buffer be be
consumed before producing more.
So I turned to asynchronous reading using BeginOutputReadLine. But this
seams to result sometimes in not all lines being read. This behaviour is not
deterministic at all. On the same input the programme will fail about half
the time.
The solution would be to either increase the console buffer to make
synchronous reading possible after the process exited or fix my code
I have one further question: When I call Start on the process which is a
command line application it causes to pop up a black command line window
that takes focus. Setting WindowStyle to ProcessWindowStyle.Hidden does not
seem to help.
Here is my code. It is F# I'm afraid but it the problem in somewhere in my
usage of the .net Framework's Process class:
let result = ref ""
let dataRecv (senderbj) (e : DataReceivedEventArgs) =
lock result (fun () -> result := !result ^ e.Data ^ "\r\n")
let dataRecvHandler = new DataReceivedEventHandler(dataRecv)
let timeOut = 1000
let discharge (str:string) =
let file = new
StreamWriter(Path.Combine(Application.StartupPath,"formula.out"),false,System.Text.Encoding.ASCII)
file.Write(str)
file.Close()
let pi = new
ProcessStartInfo(Path.Combine(Application.StartupPath,"eprover"),"--tstp-format
--cpu-limit=2 -xAuto -tAuto " ^
Path.Combine(Application.StartupPath,"formula.out"))
pi.RedirectStandardOutput <- true
pi.RedirectStandardError <- true
pi.UseShellExecute <- false
pi.WindowStyle <- ProcessWindowStyle.Hidden
let p = Process.Start(pi)
lock result (fun () -> result := "")
p.OutputDataReceived.AddHandler dataRecvHandler
p.BeginOutputReadLine()
if not(p.WaitForExit(timeOut)) then
p.Kill()
p.CancelOutputRead()
p.OutputDataReceived.RemoveHandler dataRecvHandler
p.Close()
else
p.CancelOutputRead()
p.OutputDataReceived.RemoveHandler dataRecvHandler
lock result (fun () -> result := p.StandardError.ReadToEnd() ^
!result)
p.Close()