Process.StandardOutput Freeze GUI需要轻松修复。 [英] Process.StandardOutput Freeze GUI need easy fix.

查看:79
本文介绍了Process.StandardOutput Freeze GUI需要轻松修复。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以帮我解决这个问题:



I was wondering if someone can help me out with this code:

Process VerifyProcces = new Process();
//get path here the verify.exe is
string path = Path.GetFullPath(".\\");
VerifyProcces.StartInfo.FileName = Path.Combine(path, "verify_chunk_transition.exe");

//VerifyProcces.StartInfo.CreateNoWindow = true;
VerifyProcces.StartInfo.UseShellExecute = false;
//VerifyProcces.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
VerifyProcces.StartInfo.RedirectStandardOutput = true;
VerifyProcces.StartInfo.RedirectStandardInput = true;

//set workderection to path were the chunks are
VerifyProcces.StartInfo.WorkingDirectory = _outputpath;
VerifyProcces.StartInfo.Arguments = ".\\\\ " + _framerate + " " + pid;
VerifyProcces.Start();

MyStreamReader msr_stdout = new MyStreamReader(VerifyProcces.StandardOutput);

Thread t_stdout = new Thread(msr_stdout.Go);

t_stdout.Start();
t_stdout.Join();

VerifyProcces.WaitForExit();
string output = msr_stdout.Text;
return output;





我走的问题是接口冻结,现在我试图使用背景工作但是这赢了开始多个过程...



i需要一个简单的修复程序,以便在新线程中轻松读取此过程,这不会导致我的UI崩溃。



谢谢。



Wessel



the problem i was walking in is that the interface is freezing, now i tried to use a backgroundworker but this won't start multiple process...

i need a simple fix for easy read this process out in a new thread witch won't crash my UI.

my thanks.

Wessel

推荐答案

WaitForExit 方法调用是告诉主线程等待进程退出的方法。你可以删除它,如果可能的话,保持UI线程处于活动状态。



异步读取输出:



WaitForExit method call is the one that is telling main thread to wait till the process is exited. You can remove it, if possible, to keep UI thread active.

Asynchronously reading output:

        private void button1_Click(object sender, EventArgs e)
        {

            using (Process process = new Process())
            {
                process.StartInfo.FileName = "Executable Location Here";
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.RedirectStandardInput = true;
                process.EnableRaisingEvents = true;

                process.Exited += process_Exited;
                process.OutputDataReceived += ProccesOutputDataReceived;
                process.ErrorDataReceived += ProccesErrorDataReceived;

                process.Start();

                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
            }

        }

        void process_Exited(object sender, EventArgs e)
        {
            // Handle exit here
        }

        void ProccesErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            // Handle error here
        }
void ProccesOutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            // Handle output here using e.Data
        }





上述代码可能需要针对涉及multip的复杂情况进行修改le线程,被调用进程中的快速处理和其他开销。



Code above may need modifications for complex situations involving multiple threads, quick processing in called process and other overheads.


这篇关于Process.StandardOutput Freeze GUI需要轻松修复。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆