namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Form2 fw = new Form2();
fw.Show();
Application.DoEvent();
System.Threading.Thread.Sleep(3000);
fw.Close();
}
}
}
因为System.Threading.Thread.Sleep(3000);这句阻塞了主线程,没有“人”负责绘制主窗体,所以窗体便卡在那里,在Form2里加一个Timer控件就行了,代码如下
Form1
public Form1()
{
InitializeComponent();
Form2 fw = new Form2();
fw.ShowDialog();
}
Form2:先设置好timer1的Interval=1000,Enabled =true
int times = 0;
private void timer1_Tick(object sender, EventArgs e)
{
times++;
if (times == 3)
{
this.Close();
}
}
fw.refresh();