开发者

StandardOutput ReadToEnd when process pauses by default

开发者 https://www.devze.com 2023-04-02 14:05 出处:网络
I must have looked through hundreds of articles related to the StandardOutput ReadtoEnd halting but none of them seem to answer my specific question.

I must have looked through hundreds of articles related to the StandardOutput ReadtoEnd halting but none of them seem to answer my specific question.

The scenario I have is that I'm creating a wrapper for a 3rd party console app to be used in my ASP.NET MVC application. I start the process, pass it an input string (which is a dot file in string format) and the app returns a file (pdf in this case).

The file is placed into the stdout (according to the docs) however I don't seem to be able to read the data out.

When I run the console app manually, I receive the file as text in the output window but the program never really "exits". I have to CTRL+C to end the process.

I'm guessing this is why my calls to

process.StandardOutput.ReadToEnd(); 

Just hang?

Can anyone shed a bit of light into what I'm doing wrong. I'd like to return the file that's received in the output as a byte[] from my m开发者_开发技巧ethod call.

Thanks and appologies if it seems like this is a duplicate.


There are two problems:

  1. ReadToEnd() returns a string. That's no good idea, if the data is binary
  2. ReadToEnd() doesn't return, because the program never exits

Because of this, try reading the underlying stream (process.StandardOutput.BaseStream) with the more low level stream methods like Stream.Read and detect yourself when the program finished sending its data.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号