I tried to show a winform with the code below but it opens and immediately closes. I couldn't understand the reason for this.Any ideas?
[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));
}
}
Its works fine if I replace testWind.Show() with Application.Run(testWin).
Application.Run
runs the message loop which basically handles UI events etc until all the visible windows have shut. The method blocks until the message loop has shut down.
You need that message loop to be running in order to keep the UI up, basically - whereas Show()
will just display the window, but not run any kind of message loop itself, and not block - do the Main
method completes, and the application terminates.
See more on this question at Stackoverflow