ProcessTest

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        string FileName = @"C:\Program Files (x86)\ASPECT\EA\EA.exe";

        public MainWindow()
        {
            InitializeComponent();
            this.txtID.Text = "12100";
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();//建立外部调用线程
            p.StartInfo.FileName = FileName;//要调用外部程序的绝对路径
            p.StartInfo.Arguments = "";//参数(这里就是FFMPEG的参数了)
            p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动线程(一定为FALSE,详细的请看MSDN)
            p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中(这个一定要注意,FFMPEG的所有输出信息,都为错误输出流,用StandardOutput是捕获不到任何消息的...这是我耗费了2个多月得出来的经验...mencoder就是用standardOutput来捕获的)
            p.StartInfo.CreateNoWindow = false;//不创建进程窗口
            p.ErrorDataReceived += new DataReceivedEventHandler(Output);//外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
            p.Exited += new EventHandler(pro_Exited);
            p.Start();//启动线程
        }

        static void pro_Exited(object sender, EventArgs e)
        {
            Console.WriteLine("close..");
            Console.ReadLine();
            Console.ReadLine();
        }

        static void Output(object sendProcess, System.Diagnostics.DataReceivedEventArgs output)
        {
            Console.WriteLine(output);
            if (!String.IsNullOrEmpty(output.Data))
            {
                //处理方法...
                if (output.Data.Contains("ShowRibbonBindWindow"))
                {
                    return;
                }
                Console.WriteLine(output.Data);
                Console.ReadLine();
                Console.ReadLine();
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process p = Process.GetProcessById(int.Parse(this.txtID.Text));//建立外部调用线程
            p.StartInfo.FileName = FileName;//要调用外部程序的绝对路径
            p.StartInfo.Arguments = "";//参数(这里就是FFMPEG的参数了)
            p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动线程(一定为FALSE,详细的请看MSDN)
            p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中(这个一定要注意,FFMPEG的所有输出信息,都为错误输出流,用StandardOutput是捕获不到任何消息的...这是我耗费了2个多月得出来的经验...mencoder就是用standardOutput来捕获的)
            p.StartInfo.CreateNoWindow = false;//不创建进程窗口
            p.ErrorDataReceived += new DataReceivedEventHandler(Output);//外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
            p.Exited += new EventHandler(pro_Exited);
            //p.Start();//启动线程
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process p = Process.GetProcessById(int.Parse(this.txtID.Text));//建立外部调用线程
            IntPtr intPtr = GetDesktopWindow();
            Console.WriteLine(intPtr);
        }
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);
        [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
        public static extern IntPtr GetDesktopWindow();
        [DllImport("user32.dll")]
        public static extern IntPtr GetForegroundWindow();
    }
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProcessTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string FileName = @"C:\Program Files (x86)\ASPECT\EA\EA.exe";
            try
            {
                //ProcessStartInfo info = new ProcessStartInfo();
                //info.FileName = FileName;
                //info.Arguments = "";
                //Process pro = Process.Start(info);
                //pro.Exited += new EventHandler(pro_Exited);

                //Process pro = new Process();
                //pro.StartInfo.FileName = FileName;
                //pro.EnableRaisingEvents = true;
                //pro.Exited += new EventHandler(pro_Exited);
                 //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
                //pro.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(Output);
                //pro.Start();
                //pro.WaitForExit();


                System.Diagnostics.Process p = new System.Diagnostics.Process();//建立外部调用线程
                p.StartInfo.FileName = FileName;//要调用外部程序的绝对路径
                p.StartInfo.Arguments = "";//参数(这里就是FFMPEG的参数了)
                p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动线程(一定为FALSE,详细的请看MSDN)
                p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中(这个一定要注意,FFMPEG的所有输出信息,都为错误输出流,用StandardOutput是捕获不到任何消息的...这是我耗费了2个多月得出来的经验...mencoder就是用standardOutput来捕获的)
                p.StartInfo.CreateNoWindow = false;//不创建进程窗口
                p.ErrorDataReceived += new DataReceivedEventHandler(Output);//外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
                p.Exited += new EventHandler(pro_Exited);
                p.Start();//启动线程
                //p.BeginErrorReadLine();//开始异步读取
                //p.WaitForExit();//阻塞等待进程结束
                //p.Close();//关闭进程
                //p.Dispose();//释放资源

                //string allErrorMessage  = ?? 我在这里如何得到Output 里面的所有 ouput.Data 的值呢
                //也就相当于在这里取得那个ErrorDataReceived的所有信息

            }
            catch(Exception ex)
            {

            }
            finally
            {
                Console.ReadLine();
            }
        }

        static void pro_Exited(object sender, EventArgs e)
        {
            Console.WriteLine("close..");
            Console.ReadLine();
            Console.ReadLine();
        }

        static void Output(object sendProcess, System.Diagnostics.DataReceivedEventArgs output)
        {
            Console.WriteLine(output);
            if (!String.IsNullOrEmpty(output.Data))
            {
                //处理方法...
                Console.WriteLine(output.Data);
                Console.ReadLine();
                Console.ReadLine();
            }
        }

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值