#region Using directives using System; using System.Collections.Generic; using System.Diagnostics; using System.Threading; using System.Windows.Forms; #endregion namespace ApplicationsSample { static class Program { /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Idle += App_Idle; Application.ThreadExit += App_ThreadExit; Application.ApplicationExit += App_ApplicationExit; Application.ThreadException += App_ThreadException; //// Create and show the main form modelessly //MainForm form = new MainForm(); //form.Show(); //// Run the application //Application.Run(); //// Create the main form //Form form = new MainForm(); //// Run the application until the main form is closed //Application.Run(form); //// Run the application with a context //ApplicationContext ctx = new ApplicationContext(new MainForm()); //Application.Run(ctx); //// Run the application with a custom context //TimedApplicationContext ctx = new TimedApplicationContext(new MainForm()); //Application.Run(ctx); //// Run the application with a custom context //RemotingServerApplicationContext ctx = new RemotingServerApplicationContext(new MainForm()); //Application.Run(ctx); // Run the application Application.SetCompatibleTextRenderingDefault(false); //Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException); Application.Run(new MainForm()); } static void App_Idle(object sender, EventArgs e) { Debug.WriteLine("App_Idle"); } static void App_ThreadExit(object sender, EventArgs e) { Debug.WriteLine("App_ThreadExit on thread id = " + Thread.CurrentThread.ManagedThreadId.ToString()); } static void App_ApplicationExit(object sender, EventArgs e) { Debug.WriteLine("App_ApplicationExit"); } static void App_ThreadException(object sender, ThreadExceptionEventArgs e) { // Does user want to save or quit? string msg = "A problem has occurred in this application:\r\n\r\n" + "\t" + e.Exception.Message + "\r\n\r\n" + "Would you like to continue the application so that\r\n" + "you can save your work?"; DialogResult res = MessageBox.Show( msg, "Unexpected Error", MessageBoxButtons.YesNo); // Returning continues the application if( res == DialogResult.Yes ) return; // Shut 'er down, Clancy, she's a'pumpin' mud! Application.Exit(); } } }