本文实例分析了C#中登录窗体和欢迎窗体关闭方法。分享给大家供大家参考。具体分析如下:
在c#的winform编程中,我们经常会做登录窗体或欢迎窗体,并把他们作为启动窗体。
但是,我们有可能会遇到一些问题。
请看下面的代码:
private void button1_Click(object sender, EventArgs e) { this.Close(); new Form2().Show(); }
static void Main() { Application.EnableVisualStyles();//启用可视样式 Application.SetCompatibleTextRenderingDefault(false);//将某些控件的UseCompatibleTextRendering设为默认值 Application.Run(new Form1());//这是关键 }
Application.Run(new Form1());
private void button1_Click(object sender, EventArgs e) { this.Close(); Application.Run(new Form2()); }
这是怎么回事呢?这是因为在form1所在的线程上已经运行着一个form了,运行着标准应用程序消息循环,所以就不能在这个线程上再开启一个新循环,显示一个新form了。
既然这个线程不能再来个form了,那我们就换个线程试试!最终的button1的点击事件代码如下
private void button1_Click(object sender, EventArgs e) { this.Close();new System.Threading.Thread(() => { Application.Run(new Form2());
}).Start(); }
希望本文所述对大家的C#程序设计有所帮助。