各位不好意思,这个问题拖了很久一直没解决,我用委托一直关不了窗体,我用方法二(在原程序同样的位置使用该代码段)能关闭窗体。(我的目的是顺便学学委托)
窗体FormShowGrant执行完后打开对话框FormInsertRootGrant,FormInsertRootGrant中执行任务后关闭FormShowGrant
断点运行正常,但this.Close();执行后就是不关闭对话框
方法一(委托):
FormShowGrant.cs
  public partial class FormShowGrant : Form(父窗体)
  {
  ......
  public void MyEventFunc(object sender, System.EventArgs e)
  {
  Console.WriteLine("My event is ok!");//能正确打印
  this.Close();//断点执行正确就是不关闭对话框
  }
  }
}
在FormInsertRootGrant.cs中
namespace WindowsApplication1
{
  public delegate void MyEventHandler(object sender, System.EventArgs e);
  public event MyEventHandler myevent;
  public partial class FormInsertRootGrant : Form(子窗体)
  {
  EventArgs e = new EventArgs();
  .......
  private void button1_Click(object sender, EventArgs e)
  {
  FormShowGrant fsg = new FormShowGrant();
  // 将事件添加到队列中  
  this.myevent += new MyEventHandler(fsg.MyEventFunc);
  OnMyEvent(e);
  .......
  }
  protected void OnMyEvent(System.EventArgs e)
  {
  if (myevent != null)
  myevent(this, e);//用System.EventArgs.empty代替也没解决问题
  }
  }
}
*****************************************************
方法二:
str="显示权限"
string s;
foreach(Form f in Application.OpenForms)
{
    if(s.equal(str,f.Text))//利用窗体Text属性判断是不是FormShowGrant
    {
        f.Close();
        break;
    }
}

解决方案 »

  1.   


    因为你的子窗体事件和FormShowGrant绑定了MyEventFunc,你执行Close()的时候,默认该回调函数没有执行完成,就无法销毁父窗体,必须要按顺序执行,1回调完成,2.销毁回调绑定 3子窗体销毁,4父窗体销毁
    你要记住,回调函数有同步和异步两种方式,同步方式就是我们常用的Invoke,必须等待函数执行完成,就好比我通知你去做某某事情,我会等着你做完我再回去
    而异步函数则常用begininvoke,无需等待函数执行完成即可返回,例如我来通知你去做某某事情,通知完成后,我立刻返回,不管你做事是否成功。