/// <summary>
        /// 启动进程的程序路径
        /// </summary>
        private string m_fileName = "Notepad.exe";
        void ProcessAdd(EventArgs e)
        {
            string path = Environment.CurrentDirectory + "\\MyFile";
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            string argument = System.IO.Path.Combine(path, Guid.NewGuid().ToString() + ".txt");
            if (!System.IO.File.Exists(argument))
            {
                System.IO.File.CreateText(argument);
            }
            //设置要启动的就用程序及参数
            ProcessStartInfo ps = new ProcessStartInfo(m_fileName, argument);
            ps.WindowStyle = ProcessWindowStyle.Normal;
            Process p = new Process();
            p.StartInfo = ps;
            p.Start();
            //等待启动完成,否则获取进程信息可能失败
            p.WaitForInputIdle();            //MessageBox.Show(Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(this.m_fileName)).Length.ToString());            //创建新的Process组件的数组,并将它们与指定的进程名称(Notepad)的所有进程资源相关联.
            Process[] myprocesses;
            var fileName = System.IO.Path.GetFileNameWithoutExtension(m_fileName);
            myprocesses = Process.GetProcessesByName(fileName);
            foreach (Process p1 in myprocesses)
            {
                //通过向进程主窗口发送关闭消息达到关闭进程的目的
                p1.CloseMainWindow();
                //等待1000毫秒
                //System.Threading.Thread.Sleep(1000);
                //释放与此组件关联的所有资源
                p1.Close();
            }            MessageBox.Show(Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(this.m_fileName)).Length.ToString());            
        }这段程序开始是,用记事本打开一个文本文件
然后后面那段是取得当前所有记事本的进程,然后关闭他
但程序运行手,可以开一个记事本的进程打开文件,
后面也可以取得系统所有记事本,
但是在p1.close后关没有关闭这个记本事程序
这是为什么呢?谢谢

解决方案 »

  1.   

    可以关闭的。加上Application.DoEvents()等待一下就好了:
    Application.DoEvents();
    MessageBox.Show(Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(this.m_fileName)).Length.ToString());
      

  2.   

    加Application.DoEvents()我感觉不合理
      

  3.   

    我改了程序。加多了,p1.WaitForExit(10000);
    但还是有问题在测试几十次总会有几次还是返回的是1
    ----------
    难道我的WaitForExit不是已经等待系统把记事本程序关了后再继续执行吗?
    为什么WaitForExit后再Close后,
    Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(this.m_fileName)).Length.ToString()
    还有可能得到这个进程呢?
    谢谢        /// <summary>
            /// 启动进程的程序路径
            /// </summary>
            private string m_fileName = "Notepad.exe";
            void ProcessAdd(EventArgs e)
            {
                string path = Environment.CurrentDirectory + "\\MyFile";
                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }
                string argument = System.IO.Path.Combine(path, Guid.NewGuid().ToString() + ".txt");
                if (!System.IO.File.Exists(argument))
                {
                    System.IO.File.CreateText(argument);
                }
                //设置要启动的就用程序及参数
                ProcessStartInfo ps = new ProcessStartInfo(m_fileName, argument);
                ps.WindowStyle = ProcessWindowStyle.Normal;
                Process p = new Process();
                p.StartInfo = ps;
                p.Start();
                //等待启动完成,否则获取进程信息可能失败
                p.WaitForInputIdle();            //MessageBox.Show(Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(this.m_fileName)).Length.ToString());            //创建新的Process组件的数组,并将它们与指定的进程名称(Notepad)的所有进程资源相关联.
                Process[] myprocesses;
                var fileName = System.IO.Path.GetFileNameWithoutExtension(m_fileName);
                myprocesses = Process.GetProcessesByName(fileName);
                foreach (Process p1 in myprocesses)
                {
                    //通过向进程主窗口发送关闭消息达到关闭进程的目的
                    p1.CloseMainWindow();
                    //等待1000毫秒
                    //System.Threading.Thread.Sleep(1000);
                    //释放与此组件关联的所有资源
                      p1.WaitForExit(10000);
                    p1.Close();
                }
                MessageBox.Show(Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(this.m_fileName)).Length.ToString());        }
      

  4.   

    我也试了几十次没有发现返回1。
    理论上p1.WaitForExit(10000);表示等待1秒钟notepad还没有退出的话,这个方法也就返回了,这时候GetProcessesByName还能找到这个进程。不过1秒钟notepad还不能退出是不可思议的,除非它弹出保存对话框或者你是在其它什么复杂的环境中做的测试(多线程)?
    要确保p1退出,你改成p1.WaitForExit();让它无限制等待试试。
      

  5.   

    p1.WaitForExit(10000);
    ------------------------
    我这里是10秒,我叫别人帮我测试一下
    改成p1.WaitForExit();
    也是一样,
      

  6.   

    是简单的console程序吗,有没有其它可能影响结果的代码?
      

  7.   

    http://pan.baidu.com/share/link?shareid=128753&uk=4212471801
    这个是我这个线程,没有编译出死循环和我那个记事本出现“1”的代码
    谢谢