簡體   English   中英

大型標准輸入的C#流程中斷

[英]C# Process breaks on Large Standard Input

我需要通過GnuPG加密來加密文件的內容。 為此,我正在從FILE中獲取內容,並通過StandardInput將其傳遞給Process。 對於小數據,它可以正常工作,但是當我嘗試編寫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();

請提出解決方案? 我應該分塊發送文件數據嗎???

順便說一句,您要傳遞的數據是什么? 是文本還是二進制數據? 如果是二進制,請嘗試改寫到StandardInput.BaseStream 我認為默認情況下stdin處於文本模式,如果傳遞某些字節(如EOF或NULL),則可能會損壞。請參閱https://stackoverflow.com/a/1284753/717732

編輯:

我的評論是正確的-如果子進程讀取數據的速度不夠快(或根本不讀取),則流緩沖區將被阻塞,所有寫入將被阻塞,就像在緩沖區為空時讀取阻塞一樣。 例如,請參見如何避免標准輸入卡在C#進程中

您可以輕松地驗證這一點:創建一個微型應用程序,該應用程序在無限循環中從stdin讀取。 然后,用一個小型測試應用程序臨時替換您的gpgexecutable路徑,看看是否阻塞。 它很可能不會阻塞。

您絕對應該檢查子進程為什么不讀取您嘗試發送的數據。 也許命令行參數錯誤? 或者,如果一切看起來都很好,而只是子進程運行得如此緩慢(您可以在TaskMonitor中檢查該進程的活動-gpg是否睡眠或旋轉?),然后將流編寫代碼放到某些backgroundworker中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM