public void MainForm_DestroyOldHandleWithThreadExit_CallsHandler() { var mainForm = new SubForm(); var newMainForm = new SubForm(); var context = new ApplicationContext(mainForm) { MainForm = newMainForm }; int callCount = 0; EventHandler handler = (sender, e) => { Assert.Same(context, sender); Assert.Same(EventArgs.Empty, e); callCount++; }; context.ThreadExit += handler; Assert.NotEqual(IntPtr.Zero, mainForm.Handle); Assert.Equal(0, callCount); Assert.NotEqual(IntPtr.Zero, newMainForm.Handle); Assert.Equal(0, callCount); mainForm.OnHandleDestroyed(EventArgs.Empty); Assert.Equal(0, callCount); newMainForm.OnHandleDestroyed(EventArgs.Empty); Assert.Equal(1, callCount); // Call again. mainForm.OnHandleDestroyed(EventArgs.Empty); Assert.Equal(1, callCount); newMainForm.OnHandleDestroyed(EventArgs.Empty); Assert.Equal(1, callCount); }