I found this excellent answer that is very similar to the problem I'm trying to solve:
Process sometimes hangs while waiting for Exit
My first time working with this System.Reactive package though so I'm struggling with the usage and syntax. I'm trying to modify this block to suit my needs:
var processExited =
// Observable will tick when the process has gracefully exited.
Observable.FromEventPattern<EventArgs>(process, nameof(Process.Exited))
// First two lines to tick true when the process has gracefully exited and false when it has timed out.
.Select(_ => true)
.Timeout(TimeSpan.FromMilliseconds(processTimeOutMilliseconds), Observable.Return(false))
// Force termination when the process timed out
.Do(exitedSuccessfully => { if (!exitedSuccessfully) { try { process.Kill(); } catch {} } } );
I would like this to when it times out it checks the stdOut buffer for a particular string, if it finds it then exit, otherwise continue until the next timeout. But also, I only want to do so many timeouts before I 'give up' and kill the process so in the loop, if it doesn't exit, i would increment a counter and check that. Thank you for the help
Adding clarification, I want something like this:
int timeoutCount = 0;
const int maxTimeoutCount =5;
then in the observable, something like .Do(exitedSuccessfully => {
if (!exitedSuccessfully) {
try {
if (timeoutCount >= maxTimeOutCount || output.ToString().Contains("string_i-m_looking_for_in_output_to_trigger_exit")) {
process.Kill();
}
timeOutCount++;
}
catch { }
}