从WPF应用启动和管理外部流程 [英] Start and manage external process from a WPF app

查看:102
本文介绍了从WPF应用启动和管理外部流程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序,我需要启动一个单独的MFC应用程序,然后与它进行通信.我本来打算使用Process.Start,但是我想知道这几天是否有更好的方法可以做到这一点.我可以自己研究事物,但是我需要知道从哪里开始.谢谢.

我发现这暗示没有.这是真的吗?

System.Diagnostics.Process.Start()的替代方案

谢谢.

解决方案

对于您的直接问题,.NET的最新版本中没有新的东西可以提供更好或更最新的方式来启动本地可执行文件. Process.Start是(过去一直)要走的路.

最简单,最方便的是在Process上选择五个静态方法之一.传递字符串或填充的StartInfo实例.如果您需要对流程的提出方式进行更多控制,则可以使用后者.如果您想将程序的stdio作为流通过管道传输到自己的应用程序中,则对您的情况感兴趣.这是从我的一个实用程序中填充Start Info实例的示例...

        ProcessStartInfo start = new ProcessStartInfo(BaseIoConstantsProvider.CommandProcessor)
            {
                Arguments = BaseIoConstantsProvider.KeepAlive,
                UseShellExecute = false,
                CreateNoWindow = BaseIoConstantsProvider.NoDosWindow,
                RedirectStandardOutput = true,
                RedirectStandardInput = true,
                RedirectStandardError = true,
                WindowStyle = ProcessWindowStyle.Hidden,
            };

对于问题的第二部分,如果您需要在启动过程后与该过程进行交互,则静态方法将不起作用.来自同一实用程序...

        Process p = new Process { StartInfo = start, EnableRaisingEvents = true };
        p.ErrorDataReceived += PErrorDataReceived;
        p.Exited += PExited;
        p.Start();
        p.StandardInput.AutoFlush = true;
        p.StandardInput.WriteLine(cmdLine);
        p.BeginOutputReadLine();

此示例显示了与从进程读取stdio一起引起的两个事件.这样做非常好,但是如果您只想启动另一个可执行文件,那就矫kill过正.

因此,选择启动方法的主要决定因素是:应用启动后,我的应用是否需要与流程进行交互?

最后,有时您可能想调用规范动词,甚至创建自己的动词来启动给定过程.当您右键单击某个项目时,这些内容将显示在上下文菜单中,并为您提供了更多的灵活性来启动流程.这里有一篇很棒的文章 http ://msdn.microsoft.com/zh-cn/library/windows/desktop/cc144101(v = vs.85).aspx#canonical 关于如何实现动词.

I have a WPF application and I need to spin up a separate MFC application and then communicate with it. I was going to use Process.Start, but I'm wondering if there is a better way to do this these days. I can research things myself, but I need to know where to start. Thanks.

Edits:

I found this suggesting there isn't. Is this true?

Alternatives to System.Diagnostics.Process.Start()

Thanks.

解决方案

For your immediate question, there is nothing new in the recent versions of .NET that gives a better or more up-to-date way to start a local executable. Process.Start is (and has been) the way to go.

The simplest, and most convenient, is to select one of the five static methods on Process. Passing strings or a populated StartInfo instance. You would use the latter if you needed more control over how the process got raised. Or of interest in your case, if you wanted to pipe the program's stdio as a stream into your own application. Here's a sample of populating a Start Info instance from one of my utilities...

        ProcessStartInfo start = new ProcessStartInfo(BaseIoConstantsProvider.CommandProcessor)
            {
                Arguments = BaseIoConstantsProvider.KeepAlive,
                UseShellExecute = false,
                CreateNoWindow = BaseIoConstantsProvider.NoDosWindow,
                RedirectStandardOutput = true,
                RedirectStandardInput = true,
                RedirectStandardError = true,
                WindowStyle = ProcessWindowStyle.Hidden,
            };

For the second part of your question, the static method will not do if you need to interact with the process once it is started. From the same utility...

        Process p = new Process { StartInfo = start, EnableRaisingEvents = true };
        p.ErrorDataReceived += PErrorDataReceived;
        p.Exited += PExited;
        p.Start();
        p.StandardInput.AutoFlush = true;
        p.StandardInput.WriteLine(cmdLine);
        p.BeginOutputReadLine();

This example shows a two events being hooked along with reading the stdio from the process. It's great for that purpose, but overkill if you just want to start another executable.

So the main determinate in selecting a start method is the question: does my app need to interact with the process once it's started?

And finally, sometimes you may want to invoke a canonical verb, or even create a verb of your own to start a given process. These appear in the context menu when you right click an item and give you lots of additional flexibility for starting a process. There's an excellent article here http://msdn.microsoft.com/en-us/library/windows/desktop/cc144101(v=vs.85).aspx#canonical on how to implement a verb.

这篇关于从WPF应用启动和管理外部流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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