为什么"this"指向类而不是类对象?



假设我有两个WinForms:类Form1的f1和类Form2的f2。我想做的是:单击f1上的按钮1,应用程序将处理f1并运行f2。这是代码:

//Form1
public partial class Form1 : Form
{
  public Form1()
  {
    InitializeComponent;
  }
  public delegate void EH_ChangeForm(object sender, EventArgs e);
  //this defines an event
  public static event EH_ChangeForm ChangeForm;
  private void button1_Click(object sender, EventArgs e)
  {
    //this raises the event
    ChangeForm(this, new EventArgs()); //  NRC happens here!!! Zzz~
  }
}
//Program
static class Program
{
  static Form1 f1;
  static Form2 f2;
  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    f1 = new Form1();
    f2 = new Form2();
    Application.Run(f1);
    //this subscribers to the event
    Form1.ChangeForm += Form1_ChangeForm;
  }
  static void Form1_ChangeForm(object sender, EventArgs e)
  {
    f1.Dispose();
    Application.Run(f2);
  }

}

问题是:通过单击按钮1,程序在尝试引发事件时变得糟糕(行"ChangeForm(this,new EventArgs();")。发生NullReferenceException时,"this"指向Form1而不是f1。

更一般地,我应该如何在类之间使用事件?也就是说,一个类对象应该如何订阅另一类对象引发的事件?

获得NullReferenceException的原因是没有事件处理程序注册到Form1.ChangeForm,因为Application.Run等待实例f1停止接收消息。

您需要交换Main方法中的两行,如下所示:

 Form1.ChangeForm += Form1_ChangeForm;
 Application.Run(f1);

总是试着"尽可能快"地注册事件处理程序,这样你就不会执行一些事情,也不会期望在没有人监听的情况下执行事件。

此外,在编写事件调用程序时,请尝试使用缓存事件然后调用它的模式

private void FireChangeForm() {
    var handler = ChangeForm;
    if (handler != null) {
        handler(this, new EventArgs());
    }
}

所以你也要避免任何比赛条件。请阅读埃里克·利珀特的博客文章《事件与比赛》,了解你为什么要这样做。

最新更新