C#中关闭窗体触发什么事件?

来源:百度知道 编辑:UC知道 时间:2024/07/04 10:04:57
通过窗体右上角X按钮关闭窗体,触发什么事件?
实验下来并不触发FormClosed或FormClosing事件,是怎么回事?
实验代码:
Microsoft Visual Studio 2005 C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace thread_close
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}

Thread th;

private void Form1_Load(object sender, EventArgs e)
{
th = new Thread(new ThreadStart(thStart));
th.Start();
}

private void thStart()
{
while (true)
{
Thread.Sleep(1000);
}

先触发FormClosing事件,再触发FormClosed事件。
你代码可能有问题哦~

据我所知
Thread类中的Abort方法有时候并不能立刻结束线程的
你查下MSDN里关于Abort方法的介绍吧

你可以在Form类设置一个bool类型的成员变量来标志线程是否结束
private bool flag = true;
private void thStart()
{
while (flag)
{
Thread.Sleep(1000);
}
然后在Form_Closed方法里把flag设置为false
这种方法很常用的

}

你的实验肯定出问题了兄弟,
肯定会触发这2个事件的.

建议你把实验的代码发上来看看.