c# ProcessStartInfo.Start - reading output but with a timeout

后端 未结 6 1533
小鲜肉
小鲜肉 2020-12-08 23:53

If you want to start another process and wait (with time out) to finish you can use the following (from MSDN).

//Set a time-out value.
int timeOut=5000;
//Ge         


        
6条回答
  •  青春惊慌失措
    2020-12-09 00:51

    You could also use the APM, like this:

    Define a delegate for the ReadToEnd call:

    private delegate string ReadToEndDelegate();
    

    Then use the delegate to call the method like this:

    ReadToEndDelegate asyncCall = reader.ReadToEnd;
    IAsyncResult asyncResult = asyncCall.BeginInvoke(null, null);
    asyncResult.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(10));
    asyncCall.EndInvoke(asyncResult);
    

    EDIT: Error handling removed for clarity.

提交回复
热议问题