C# 在启动批处理流时冻结
嘿人们!我正在使用名为 MineMe 的小工具,它用于处理 Minecraft 服务器。
所以我创建了一个文件流,它应该流式传输 start_base.cmd (启动服务器的文件)的输出。出了什么问题,我的表单窗口冻结了,直到我终止进程(java.exe - 由 start_base.cmd 运行)
这是我的代码:
ProcessStartInfo processInfo = new System.Diagnostics.ProcessStartInfo("CMD");
processInfo.WindowStyle = ProcessWindowStyle.Normal;
processInfo.RedirectStandardOutput = true;
processInfo.RedirectStandardInput = true;
processInfo.RedirectStandardError = true;
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
Process p = new Process();
p.StartInfo = processInfo;
p.Start();
TextWriter tw = p.StandardInput;
tw.Flush();
tw.WriteLine("start_base.cmd");
tw.Close();
TextReader tr = p.StandardOutput;
string output = tr.ReadLine();
while (output != null)
{
this.lg_log.Items.Add(output); // add the output string to a list box
output = tr.ReadLine();
}
这里出了什么问题? :) 请帮我 ..
Hey people! I am working with my little tool called MineMe, and it is used to handle Minecraft servers.
So i made a file stream, that should stream the output of the start_base.cmd (the file that starts the server). What goes wrong, is that the window with my form freezes, until i kill the process (java.exe - Ran by start_base.cmd)
Here is my code:
ProcessStartInfo processInfo = new System.Diagnostics.ProcessStartInfo("CMD");
processInfo.WindowStyle = ProcessWindowStyle.Normal;
processInfo.RedirectStandardOutput = true;
processInfo.RedirectStandardInput = true;
processInfo.RedirectStandardError = true;
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
Process p = new Process();
p.StartInfo = processInfo;
p.Start();
TextWriter tw = p.StandardInput;
tw.Flush();
tw.WriteLine("start_base.cmd");
tw.Close();
TextReader tr = p.StandardOutput;
string output = tr.ReadLine();
while (output != null)
{
this.lg_log.Items.Add(output); // add the output string to a list box
output = tr.ReadLine();
}
What's wrong here? :) Please help me ..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在你的 UI 线程上启动另一个线程来处理 while 循环:
On your UI thread start another thread to process the while loop:
问题是你的
while
循环。您需要在单独的线程(即不是您的 UI 线程)上执行此操作。如果您通过单击按钮(或其他一些 UI 控件)调用上述代码,则应该使用 BackgroundWorker 线程或线程池中的线程(甚至只是普通线程)来执行此任务。
The problem is your
while
loop. You need to do this on a separate thread (i.e. not your UI thread).If you're calling the above code from a button click (or some other UI control), you should instead use a BackgroundWorker thread or a thread from the thread pool (or even just a plain vanilla Thread) for this task.