本帖最后由 nielao 于 2009-09-01 17:56:06 编辑

解决方案 »

  1.   

    lock
    return
    break有什么用呢?
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        delegate void HandleNotePadDelegate();
        delegate void EnableTheButton1Delegate();    public partial class Form1 : Form
        {
            private HandleNotePadDelegate h;
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    this.button1.Enabled = false;
                    h = new HandleNotePadDelegate(this.HandleNotePad);
                    h.BeginInvoke(this.EndAyncCall, null);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }        private void HandleNotePad() 
            {
                //结束某些进程
                foreach (System.Diagnostics.Process thisproc in System.Diagnostics.Process.GetProcesses())
                {
                    if (thisproc.ProcessName.Equals("notepad"))
                    {
                        thisproc.CloseMainWindow();
                        thisproc.WaitForExit(1000);
                        thisproc.Close();                    System.Threading.Thread.Sleep(2000);
                    }
                }
                
                System.Diagnostics.Process.Start("notepad");
            }        private void EndAyncCall(System.IAsyncResult ar)
            {
                try
                {
                    h.EndInvoke(ar);
                }
                catch{ }
                finally 
                {
                    EnableTheButton1Delegate h2 = new EnableTheButton1Delegate(this.EnableButton1);
                    this.Invoke(h2);
                }
            }        private void EnableButton1()
            {
                this.button1.Enabled = true;         }    }}
      

  3.   

    刚才试了一下,上面贴出的代码似乎能用。
    请lz参考一下吧。楼主贴出的代码完全是同步调用。禁用button1、关闭进程和打开进程三个动作都在UI线程里执行的话,当其中某个动作需要较长时间的时候就容易导致窗口停滞。上面的代码大致是按照如下思路来做的——
    (1)把关闭进程和打开进程两个可能耗时较长的动作放到一个辅助线程里去异步执行:
         h = new HandleNotePadDelegate(this.HandleNotePad); 
         h.BeginInvoke(this.EndAyncCall, null); (2)开启异步操作之前先行禁止button1:this.button1.Enabled = false; (3)异步操作结束时将回调EndAyncCall方法,在该方法里解除针对button1的禁用:
         EnableTheButton1Delegate h2 = new EnableTheButton1Delegate(this.EnableButton1); 
         this.Invoke(h2); 
      

  4.   

     //启动某进程
    foreach (System.Diagnostics.Process thisproc in System.Diagnostics.Process.GetProcesses())
    {
      if (!thisproc.ProcessName.Equals("notepad"))
      {
         System.Diagnostics.Process.Start("notepad");
         button1.Enabled = true;
         break;
      }
    }lz上面的这段代码有点看不懂。
    既然只想保留一个notepad进程,为何要循环操作呢?
      

  5.   

    using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;namespace L2Restart
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        static int i = 0;        private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    foreach (System.Diagnostics.Process thisproc in System.Diagnostics.Process.GetProcesses())
                    {
                        if (thisproc.ProcessName.Equals("taskmgr"))
                        {
                            i++;
                        }
                    }
                    if (i > 0)
                    {
                        foreach (System.Diagnostics.Process thisproc in System.Diagnostics.Process.GetProcesses())
                        {
                            if (thisproc.ProcessName.Equals("taskmgr"))
                            {
                                i--;
                                if (!thisproc.CloseMainWindow())
                                    thisproc.Kill();
                                thisproc.WaitForExit(1000);
                                thisproc.Close();                            System.Threading.Thread.Sleep(2000);
                            }
                        }
                        if (i==0)
                            System.Diagnostics.Process.Start("taskmgr");
                    }
                    else
                        System.Diagnostics.Process.Start("taskmgr");
                }
                catch (Exception ex)
                {
                    throw ex;
                }        }
        }
    }谢谢楼上的,我改成这样啦,但是连续快速点击还是有点问题,我再测试下你的方案!