www.pudn.com > sudoku.rar > Program.cs


//-------------------------------------------------------------------------- 
//  
//  Copyright (c) Microsoft Corporation.  All rights reserved.  
//  
//  File: Program.cs 
// 
//  Description: Application entry-point. 
//  
//-------------------------------------------------------------------------- 
 
using System; 
using System.Security; 
using System.Windows.Forms; 
using System.Security.Permissions; 
using System.Diagnostics; 
using System.Threading; 
using System.Globalization; 
using System.Runtime.InteropServices; 
using System.Runtime.CompilerServices; 
using Microsoft.Sudoku.Utilities; 
 
namespace Microsoft.Sudoku 
{ 
	/// Contains the entrypoint for the application. 
	public sealed class Program 
	{ 
		/// Prevent external instantiation. 
		private Program(){} 
 
		/// The main application form. 
		private static MainForm _mainForm; 
 
		/// Main entrypoint. 
		[STAThread] 
		public static int Main() 
		{ 
			// Setup visual styles. Do this first so that any error message boxes 
			// displayed get the visual styles to match. 
			Application.EnableVisualStyles(); 
			Application.DoEvents(); 
 
			// Verify that you have enough permissions to run.  An assembly level demand 
			// ensures that you at least have permissions to put up a message box.  That way, 
			// if you get here and fail full trust checks, you can at least tell the user what is wrong. 
			if (!HasFullTrust) 
			{ 
				MessageBox.Show(ResourceHelper.LacksMinimumPermissionsToRun, ResourceHelper.DisplayTitle, 
					MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 
					new MessageBoxOptions()); 
				return 1; 
			} 
 
			// Verify you are running on a Tablet, or that you at least have the Tablet APIs installed 
			bool validSystem = PlatformDetection.InkAssemblyAvailable && PlatformDetection.RecognizerInstalled; 
			if (!validSystem) 
			{ 
				MessageBox.Show(ResourceHelper.NotRunningOnTablet, ResourceHelper.DisplayTitle, 
					MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 
					new MessageBoxOptions()); 
				return 1; 
			} 
 
			// Run the main app 
			try 
			{ 
				return MainInternal() ? 0 : 1; 
			} 
			catch(Exception exc) 
			{ 
				ShutdownOnException(new UnhandledExceptionEventArgs(exc, false)); 
				return 1; 
			} 
			catch 
			{ 
				ShutdownOnException(new UnhandledExceptionEventArgs(null, false)); 
				return 1; 
			} 
		} 
 
		/// Run the application. 
		/// true if successful execution; otherwise, false. 
		private static bool MainInternal() 
		{ 
			// Register for unhandled exceptions 
			new SecurityPermission(SecurityPermissionFlag.ControlAppDomain).Demand(); 
			AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); 
			Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException); 
 
			// Either run the app or highlight an already existing instance of the app 
			using(SingleInstance si = new SingleInstance()) 
			{ 
				if (si.IsFirst) 
				{ 
					// Make sure several threads are available for faster initial response 
					int workerThreads, ioThreads; 
					ThreadPool.GetMinThreads(out workerThreads, out ioThreads); 
					const int minThreadsToMakeAvailable = 2; 
					if (workerThreads < minThreadsToMakeAvailable) 
					{ 
						ThreadPool.SetMinThreads(minThreadsToMakeAvailable, ioThreads); 
					} 
 
					// Create the main form and run the app 
					_mainForm = new MainForm(); // purposely not using 'using' as, in the event of an exception, want to access MainForm's state 
					Application.Run(_mainForm); 
					_mainForm.Dispose(); 
					_mainForm = null; 
 
					// Finished game successfully 
					return true; 
				} 
				else 
				{ 
					// Not the first Sudoku instance... show the other one 
					si.BringFirstToFront(); 
					return false; 
				} 
			} 
		} 
 
		/// Handles an unhandled exception on a Windows Forms thread. 
		private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) 
		{ 
			ShutdownOnException(new UnhandledExceptionEventArgs(e.Exception, false)); 
		} 
 
		/// Handles an unhandled exception from the domain. 
		private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) 
		{ 
			ShutdownOnException(e); 
		} 
 
		/// Display an error and shutdown when an exception is raised. 
		internal static void ShutdownOnException(UnhandledExceptionEventArgs e) 
		{	 
			try 
			{ 
				// Log it 
				new PermissionSet(PermissionState.Unrestricted).Demand(); 
				string message = (e.ExceptionObject != null) ? 
					e.ExceptionObject.ToString() :  
					ResourceHelper.ShutdownOnError; 
				EventLog.WriteEntry(ResourceHelper.DisplayTitle, message, EventLogEntryType.Error); 
			} 
			catch {} // You are about to exit anyway, so it is ok to eat any exceptions  
 
			try 
			{ 
				// Tell the user something bad happened 
				MessageBox.Show(ResourceHelper.ShutdownOnError, ResourceHelper.DisplayTitle, MessageBoxButtons.OK,  
					MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1, MainForm.GetMessageBoxOptions(null)); 
			}  
			catch {} // You are about to exit anyway, so it is ok to eat any exceptions  
			 
			try 
			{ 
				// If we can save the user's game, do it 
				if (_mainForm != null) _mainForm.SaveStateFile(); 
			}  
			catch {} // You are about to exit anyway, so it is ok to eat any exceptions  
 
			// exit application 
			if (!e.IsTerminating) Environment.Exit(1); 
		} 
 
		/// Determines whether the app is running with full trust. 
		private static bool HasFullTrust 
		{ 
			get 
			{ 
				try 
				{ 
					new PermissionSet(PermissionState.Unrestricted).Demand(); 
					return true; 
				} 
				catch (SecurityException) { return false; } 
			} 
		} 
	} 
}