Process readtoend waitforexit

Process readtoend waitforexit

Author: namdom On: 13.06.2017

I know that the output from the process I am starting is around 7MB long. Running it in the Windows console works fine.

Unfortunately programmatically this hangs indefinitely at WaitForExit. Note also this does code NOT hang for smaller outputs like 3KB. Is it possible that the internal StandardOutput in ProcessStartInfo can't buffer 7MB? If so, what should I do instead? If not, what am I doing wrong?

Whatever order you use, there can be a problem:. The solution is to use asynchronous reads to ensure that the buffer doesn't get full. To avoid any deadlocks and collect up all output from both StandardOutput and StandardError you can do this:.

See answers below for to handle the timeout and avoid ObjectDisposeException exceptions. The documentation for Process. StandardOutput says to read before you wait otherwise you can deadlock, snippet copied below:.

Mark Byers' answer is excellent, but I would just add the following: If the process continues to output data after the timeout has been exceeded and then terminates, the outputWaitHandle and errorWaitHandle variables will be accessed after being disposed. The problem with unhandled ObjectDisposedException happens when the process is timed out.

In such case the other parts of the condition:. This is what we've seen MS recommend. A note of caution with the Mark Byers solution:. Rob answered it and saved me few more hours of trials. I redirected both input, output and error and handled reading from output and error streams.

This solution works for SDK 7- 8. This is a more modern awaitable, Task Parallel Library TPL based solution for.

c# - ProcessStartInfo hanging on "WaitForExit"? Why? - Stack Overflow

I was having the same issue, but the reason was different. It would however happen under Windows 8, but not under Windows 7. The following line seems to have caused the problem.

The solution was to NOT disable UseShellExecute. I now received a Shell popup window, which is unwanted, but much better than the program waiting for nothing particular to happen. So I added the following work-around for that:. I tried to make a class that would solve your problem using asynchronous stream read, by taking in account Mark Byers, Rob, stevejay answers. Doing so I realised that there is a bug related to asynchronous process output stream read. I reported that bug at Microsoft: You will receive System.

StandardOut has not been redirected or the process hasn't started yet.

Doing so, make a race condition because the output stream can receive data before you set it to asynchronous:. Then some people could say that you just have to read the stream before you set it to asynchronous.

But the same problem occurs.

There will be a race condition between the synchronous read and set the stream into asynchronous mode. There is no way to acheive safe asynchronous read of an output stream of a process in the actual way "Process" and "ProcessStartInfo" has been designed. You are probably better using asynchronous read like suggested by other users for your case. But you should be aware that you could miss some information due to race condition.

Rob solution hangs and 'Mark Byers' solution get the disposed exception. I tried the "solutions" of the other answers. Currently accepted answer doesn't work throws exception and there are too many workarounds but no complete code.

pupuzifecose.web.fc2.comrdError Property (pupuzifecose.web.fc2.comstics)

This is obviously wasting lots of people's time because this is a popular question. Combining Mark Byers' answer and Karol Tyl's answer I wrote full code based on how I want to use the Process. I know that this is supper old but, after reading this whole page none of the solutions was working for me, although I didn't try Muhammad Rehan as the code was a little hard to follow, although I guess he was on the right track.

process readtoend waitforexit

When I say it didn't work that's not entirely true, sometimes it would work fine, I guess it is something to do with the length of the output before an EOF mark.

Anyway, the solution that worked for me was to use different threads to read the StandardOutput and StandardError and write the messages. This post maybe outdated but i found out the main cause why it usually hang is due to stack overflow for the redirectStandardoutput or if you have redirectStandarderror. As the output data or the error data is large, it will cause a hang time as it is still processing for indefinite duration.

By posting your answer, you agree to the privacy policy and terms of service. Stack Overflow Questions Developer Jobs Documentation beta Tags Users. Sign up or log in to customize your list. Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us.

Log In Sign Up. Join the Stack Overflow Community. Stack Overflow is a community of 7. Join them; it only takes a minute: I have the following code: Join " ", args ; info.

Start info ; p. Kara 3, 8 33 I run into same issue and this how I was able to solve it stackoverflow. It's in the manual. Whatever order you use, there can be a problem: If you wait for the process to exit before reading StandardOutput the process can block trying to write to it, so the process never ends.

If you read from StandardOutput using ReadToEnd then your process can block if the process never closes StandardOutput for example if it never terminates, or if it is blocked writing to StandardError. To avoid any deadlocks and collect up all output from both StandardOutput and StandardError you can do this: BeginErrorReadLine ; if process. Ryan 2, 6 27 Mark Byers k Had no idea redirecting the output was causing the issue but sure enough it was.

pupuzifecose.web.fc2.comrdOutput Property (pupuzifecose.web.fc2.comstics)

Spent 4 hours pounding my head on this and fixed it in 5 minutes after reading your post. AlexPeck The issue was running this as a console app. Hans Passant identified the issue here: An unhandled exception of type"System. ObjectDisposed" occurred in mscorlib. We had a similar problem as described by user above. Do you think it is possible that using statements for the event handlers need to be above the using statement for the process itself?

StandardOutput says to read before you wait otherwise you can deadlock, snippet copied below: I was in a similar situation. I was redirecting StandardError for no reason when converting with ffmpeg in a process, it was writting enough in the StandardError stream to create a deadlock.

Function timeout in pupuzifecose.web.fc2.comEnd()

This still hangs for me even with redirecting and reading standard output. FYI I had to add this caveat as an answer as I couldn't comment on his post.

Adding Mark's edited code to this answer would be rather awesome! I am having the exact same issue at the minute. Apr 3 '13 at In such case the other parts of the condition: WaitOne timeout are not executed. I resolved this problem in a following way: WaitOne timeout ; errorWaitHandle. Karol Tyl 1 8. Why have you changed output and error to outputBuilder?

Can someone please provide complete answer that works? We have this issue as well or a variant. WaitForExit nnnn ; where nnnn is in milliseconds. A note of caution with the Mark Byers solution: Simply by moving the using up higher in the code. I create a new proc in a using after the handle usings. See stevejay's answer above for an explanation why this can happen and how to solve this.

Jon 1 6. I solved it this way: WaitForExit ; I redirected both input, output and error and handled reading from output and error streams. Elina Maliarsky 11 1. There are some notes at the bottom of this MSDN doc msdn. It's hard to tell if your solution is susceptible to this issue. WriteLine "Process Timed Out! WhenAll tasks ; return process. Muhammad Rehan Saeed 7, 5 67 I thing that this is simple and better approach we don't need AutoResetEvent: WriteLine Command ; process.

AppendLine " GGSCIShell TIMEOUT: True, but shouldn't you be doing. Your solution does not need AutoResetEvent but you poll.

When you do poll instead of using event when they are available then you are using CPU for no reason and that indicate that you are a bad programmer. Your solution is really bad when compared with the other using AutoResetEvent.

But I did not give you -1 because you tried to help! So I added the following work-around for that: Hidden Now the only thing bothering me is to why this is happening under Windows 8 in the first place. You can't do that: Start ; You will receive System. BeginOutputReadLine ; Doing so, make a race condition because the output stream can receive data before you set it to asynchronous: Eric Ouellet 4, 2 44 None of the answers above is doing the job.

So I decided to suggest another solution: Introduction Currently accepted answer doesn't work throws exception and there are too many workarounds but no complete code.

Usage I have used it to create progress dialog around git commands. This is how I've used it: Set ; else stdout. Set ; else stderr. ToString ; if combineStdoutAndStderr result. WaitOne timeoutInMs ; errorWaitHandle. Timer 50 ; flushTask. TryDequeue out line sw.

春のえむじぃーお2

Alexis Coles 2 Else indeed we can just leave them to false. Sign up or log in StackExchange. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. In it, you'll get: The week's top questions and answers Important community announcements Questions that need answers.

Stack Overflow works best with JavaScript enabled. WaitForExit ; share improve this answer. Perhaps it would be better to call CancelOutputRead? This just happened to me, I solved this by giving the handles more lifetime than the process.

You need UseShellExecute to be set to false if you want to redirect the output. The problem is that people explicitly set those to true because they want to be able to access those streams! MathOverflow Mathematics Cross Validated stats Theoretical Computer Science Physics Chemistry Biology Computer Science Philosophy more 3. Meta Stack Exchange Stack Apps Area 51 Stack Overflow Talent.

inserted by FC2 system