帮别人问的问题,我也不知道为何问这么怪的问题。她是把VS默认新建窗口工程的代码给改了using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());
            Form a = new Form();
            a.Show();  用这句,运行时窗口一闪就完了
            //a.ShowDialog();
    用这句,就不会消失    }
    }
}个人猜测,会不会Show()不阻塞线程?? 我查了MSDN没找到相关说明。如果有可能,请附上MSDN的解释,谢谢

解决方案 »

  1.   


                Form a = new Form(); 
                a.Visible = true; 
                a.Show(); 
     
      

  2.   

                Form a = new Form(); 
                a.Visible = true
                a.Show(); 
      

  3.   

    启动必须放到Application.Run(new Form1());这里
      

  4.   

    没错,就是这个,执行完后那个线程直接运行到结束了,既然线程结束了,那么线程上面打开的那个Form自然也就没了。通过Application.Run打开的窗口可以保证线程在窗口关闭之前不会结束。
      

  5.   

    Application.Run(Form) 作用是在当前线程上开始运行标准应用程序消息循环,并使指定窗体可见。
    Application.Run 直到指定窗体关闭才退出,所以new的临时变量可以一直不被回收。
      

  6.   

    7楼的方法就不会消失了
    using System;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
                Form b= new Form();
                Application.Run(b);
            }
        }
    }
      

  7.   

    让b窗体显示在前边using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                Form b = new Form();
                Application.Run(b);
                InitializeComponent();        }
        }
    }
      

  8.   

    show()不阻塞线程,那么,Main函数运行show()后,即结束整个程序,而showDialog()和Application.Run()均会阻止当前Main函数继续执行。