0
私のWindowsフォームプログラムでは、ライブでバッチファイルを読みたいと思っています。検索した後、私はそれを行う方法を見つけた。しかし、それはコンソールでのみ動作します。 console.writeファイルをrichtextbox.appendtextなどで置き換えると、 "InvalidOperationException"と表示されます。ライブのバッチファイルをリッチテキストボックスで読む
はここ
var process = new Process
{
StartInfo =
{
WorkingDirectory = @"C:\Users\Rayan\Desktop",
FileName = "cmd.exe",
Arguments = "/c test.bat",
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false
}
};
process.OutputDataReceived += (s, e) => { richTextBox1.AppendText(e.Data + "\n"); };
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
私は多くのことをテストしたが、何も動作私のコードです。ご協力ありがとうございます。 Polymeth。
OutputDataReceivedは、コントロールに対してスレッドセーフな呼び出しを行うために必要なUIとは別のスレッドで発生します。ここであなたとほぼ同じ質問があります。https://stackoverflow.com/questions/5051550/cross-thread-problem – Bearcat9425
[クロススレッド問題](https://stackoverflow.com/questions/5051550/cross-thread)の可能な複製-問題) – Bearcat9425