【发布时间】:2014-04-07 07:53:14
【问题描述】:
我试图用下面的代码显示一个winform,但它会打开并立即关闭。 我不明白这是为什么。有什么想法吗?
[STAThread]
static void Main()
{
try
{
AppDomain.CurrentDomain.UnhandledException += AllUnhandledExceptions;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
Test testWin = new Test();
testWin.Show();
}
catch (Exception ex)
{
Logger.Error("Main : " + ex.Message, typeof(Program));
}
}
如果我用 Application.Run(testWin) 替换 testWind.Show() 效果很好。
【问题讨论】:
-
它“打开”得很好,因为程序在一微秒后终止,所以无法看到它。 Application.Run() 是 GUI 应用程序中的硬性要求,它解决了producer-consumer problem。生产者是操作系统,消费者是你的 UI。你什么都不吃。