简体   繁体   English

大型标准输入的C#流程中断

[英]C# Process breaks on Large Standard Input

I need to Encrypt the content of file by GnuPG encryption. 我需要通过GnuPG加密来加密文件的内容。 for that, I am fetching content from FILE and passing it Process via StandardInput. 为此,我正在从FILE中获取内容,并通过StandardInput将其传递给Process。 For small data it is working fine but when I try to Write StandardInput of more than 500KB data, program gets stuck. 对于小数据,它可以正常工作,但是当我尝试编写500KB以上数据的StandardInput时,程序将卡住。

    process = new Process();
     process.StartInfo.WorkingDirectory = _bindirectory;
     process.StartInfo.RedirectStandardInput = true;
     process.StartInfo.RedirectStandardOutput = true;
     process.StartInfo.RedirectStandardError = true;
     process.StartInfo.FileName = gpgExecutable;
     process.StartInfo.Arguments = gpgOptions;
     process.StartInfo.UseShellExecute = false;
     process.StartInfo.CreateNoWindow = true;

     process.Start();
/*****Here is the line where Compiler Process Stucks on large data****/
     process.StandardInput.Write(inputText); 
     process.StandardInput.Flush();
     process.StandardInput.Close();

     process.OutputDataReceived += process_OutputDataReceived;
     process.ErrorDataReceived += process_ErrorDataReceived;
     process.BeginErrorReadLine();
     process.BeginOutputReadLine();
     process.WaitForExit();

Please suggest a solution? 请提出解决方案? Should i send data of file in chunks??? 我应该分块发送文件数据吗???

By the way, what is the data that you are trying to pass through? 顺便说一句,您要传递的数据是什么? is it text or binary data? 是文本还是二进制数据? If binary, try writing to the StandardInput.BaseStream instead. 如果是二进制,请尝试改写到StandardInput.BaseStream I think that stdin is in textual mode by default and can get broken if passed some certain bytes like EOF or NULL.. see https://stackoverflow.com/a/1284753/717732 我认为默认情况下stdin处于文本模式,如果传递某些字节(如EOF或NULL),则可能会损坏。请参阅https://stackoverflow.com/a/1284753/717732

Edit: 编辑:

I as right in my comment - if the child process does not read the data fast enough (or doesn't read at all) the stream buffer will get choked and all writes will block, just like reads block when the buffer is empty. 我的评论是正确的-如果子进程读取数据的速度不够快(或根本不读取),则流缓冲区将被阻塞,所有写入将被阻塞,就像在缓冲区为空时读取阻塞一样。 See for example How to avoid standard input getting stuck in c# process 例如,请参见如何避免标准输入卡在C#进程中

You may verify this easily: create a tiny application which reads from stdin in an infinite loop. 您可以轻松地验证这一点:创建一个微型应用程序,该应用程序在无限循环中从stdin读取。 Then, temporarily replace your gpgexecutable path with a that one tiny test app and see if blocks or not. 然后,用一个小型测试应用程序临时替换您的gpgexecutable路径,看看是否阻塞。 It most probably will not block. 它很可能不会阻塞。

You should definitely check why the child process is not reading the data you try to send. 您绝对应该检查子进程为什么不读取您尝试发送的数据。 Maybe commandline parameters are wrong? 也许命令行参数错误? Or if everything seems fine and it is just the childprocess working so slowly (you can check the process' activity in TaskMonitor - does the gpg sleep or spin?), then put the stream-writing code into some backgroundworker.. 或者,如果一切看起来都很好,而只是子进程运行得如此缓慢(您可以在TaskMonitor中检查该进程的活动-gpg是否睡眠或旋转?),然后将流编写代码放到某些backgroundworker中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM