重定向过程状态 [英] redirection of processstatus

查看:84
本文介绍了重定向过程状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码执行cpp文件.

I am using following code to execute cpp files.

public void ExecuteCommandSync(string command)
        {
            try
            {
                MessageBox.Show(command);
                                // create the ProcessStartInfo using "cmd" as the program to be run, and "/c " as the parameters.
                // Incidentally, /c tells cmd that we want it to execute the command that follows, and then exit.

                System.Diagnostics.ProcessStartInfo procStartInfo = new
                    System.Diagnostics.ProcessStartInfo("cmd", "/c" + command);
                // The following commands are needed to redirect the standard output. 
                ////This means that it will be redirected to the Process.StandardOutput StreamReader.
                procStartInfo.RedirectStandardOutput = true;
                procStartInfo.UseShellExecute = false;
                // Do not create the black window.
                procStartInfo.CreateNoWindow = true;
                // Now we create a process, assign its ProcessStartInfo and start it
                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                proc.StartInfo = procStartInfo;
                proc.Start();
                // Get the output into a string
                string result = proc.StandardOutput.ReadToEnd();
                // Display the command output.
                //obj.listBox1.Items.Add(result);                
            }
            catch (Exception objException)
            {
                MessageBox.Show(objException.Message);


            }
        }


我想在应用程序主页的列表框中显示执行状态(成功或编译错误).请在这方面帮助我.


and i want to display the status of execution(success or compilation errors) to a listbox in the home page of the application. please help me in this regard.

推荐答案

您无法执行cpp文件",只能执行(某些)可执行文件.

据我了解,您正在尝试进行编译.

我可以看到的一个问题是:您不应该使用"cmd""/c".您应仅使用命令",并使用适当的命令行参数(包括输入文件).其他所有内容都取决于您的编译方式,使用的编译器,构建项目文件或解决方案(可以使用MSBuild.exe完成)等.

另一个问题是:StandardOutput的重定向并不总是足够的.通常,您还需要重定向StandardError;这是完全一样的方法.希望这能解决您的问题.



您试图重新发布此问题.请不要这样做-它仍然无法帮助您.相反,请使用同一页面,在本例中为以下页面:使用上面的改进问题",对问题/答案发表评论,回复现有评论.



响应OP的示例代码对此答案的评论:

首先,永远不要使用重复的字符串串联(+).您是否知道字符串是不可变的?在您的情况下,请使用string.Format.在其他情况下(例如循环),请使用System.Text.StringBuilder.

我不知道什么是ExecuteCommandSync.您可以使用静态方法Process.Start(ProcessStartInfo)或实例方法Process.Start(),并将适当的StartInfo分配给Process的实例.要进行同步,请使用实例方法Process.WaitForExit.

最好在单独的线程中执行此操作,并将其输出与UI线程同步.请查看我过去有关UI线程调用的解决方案:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5问题 [StandardOutput或StandardError的文本输出中报告错误.您需要同时重定向.毕竟,手动运行编制命令-您应该在重定向的流中获取所有输出,并进行检查.

毕竟,请参见
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx [ ^ ] —但彻底.

我很好奇:您还要运行链接器吗?

我已经做到了像Visual Studio这样的UI.



还有一件事.我注意到您正在对文件路径名进行硬编码.希望这只是在开发过程中用于实验目的.
在任何情况下,硬编码的路径名都是有用的.您应该始终计算文件的路径名,从配置文件中获取它,等等.

—SA
You cannot "execute cpp files", you can only execute (some) executable files.

As far as I can understand, you are trying to compile.

One problem I can see is this: you should not use "cmd" "/c". You should use "command" only, with proper command line parameters including input file(s). Everything else depends on how you compile, what compiler do you use, do you build a project file or solution (which you could do with MSBuild.exe), etc.

Another problem is: redirecting of StandardOutput is not always enough. Generally, you also need to redirect StandardError; this is done in exact same way. I hope this will resolve your problem.



You tried to re-post this question. Please don''t do it — it cannot help you anyway. Instead, use the same page, in this case, this page: use "Improve question" above, comment on question/answer, reply to existing comments.



In response to code sample of the OP''s comment to this answer:

First of all, never ever use repeated string concatenation (+). Do you know that strings are immutable? In your case, use string.Format. In other cases (like loops), use System.Text.StringBuilder.

I have no idea what is ExecuteCommandSync. You can use the static method Process.Start(ProcessStartInfo) or the instance method Process.Start() with appropriate StartInfo assigned to the instance of Process. For synchronization, use the instance method Process.WaitForExit.

It''s the best to do it in a separate thread and synchronized its output with your UI thread. Please see my past solutions on UI thread invocation:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

If is not clear: "unable to handle exceptional cases like errors in cpp file". There are no "exceptional cases like errors" in CPP files. All you have is build errors. Exceptions are not thrown. Errors are reported in text output which is either StandardOutput or StandardError. You need to redirect both. After all, run compilation command manually — you should get all the output in your redirected streams, check it up.

After all, see http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^] — but thoroughly.

I''m just curious: are you going to run linker as well?

Making an UI like Visual Studio is quite possible, I''ve done it.



One more thing. I noticed you are hard-coding file path names. Hope this is only for experimental purposes during development.
There are no situations where a hard-coded path name can be useful. You should always calculate path names for files, get it from configuration files, etc.

—SA


这篇关于重定向过程状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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