Click here to Skip to main content
15,892,537 members
Articles / Desktop Programming / Windows Forms

Ye Aulde Application Starter

Rate me:
Please Sign up or sign in to vote.
4.45/5 (13 votes)
3 Dec 20057 min read 54.4K   894   72  
Give your application a good start! Handle unhandled exceptions, ensure one instance running, use splash screen for lengthy load and more. All in one pack - enjoy!
using System;
using System.Threading;
using System.Reflection;
using System.Windows.Forms;

namespace AppStart {

	public class MainForm : System.Windows.Forms.Form {

		#region Windows Form Designer generated code

		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label1;

		private void InitializeComponent() {
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
			this.button1 = new System.Windows.Forms.Button();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.label1 = new System.Windows.Forms.Label();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.button1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.button1.Location = new System.Drawing.Point(188, 240);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(164, 32);
			this.button1.TabIndex = 0;
			this.button1.Text = "Simulate unexpected error";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox1.Location = new System.Drawing.Point(12, 12);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(516, 212);
			this.groupBox1.TabIndex = 2;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "GroupBox";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 28);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(160, 20);
			this.label1.TabIndex = 2;
			this.label1.Text = "Label";
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(540, 278);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.button1);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "MainForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Application Starter";
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}

		protected override void Dispose( bool disposing ) {
			if( disposing ) {
				if (components != null) {
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#endregion

		[STAThread]
		static void Main() {
			// Enable XP themed controls
			Application.EnableVisualStyles();
			Application.DoEvents();
			// ensure one instance running
			if(!MainForm.InstanceExists) {
#if DEBUG
				Application.Run(new MainForm());
#else
				// enable unhandled exceptions trapping for release version
				try {
					Application.Run(new MainForm());
				}
				catch(Exception ex) {
					new ExceptionDialog(ex).ShowDialog();
				}
#endif
			}
		}

		static Mutex mutex;
		static string AppGuid = "4B8CB7C2-1BD3-4db4-AFF0-A398466C5EEF";
		static bool InstanceExists {
			get {
				bool notExists;
				mutex = new Mutex( false, "Local\\" + AppGuid + " - one instance runner.", out notExists ); 
				return !notExists;
			}
		} 

		public MainForm() {
			Splash.Show(this);
			Thread.Sleep(2000);
			InitializeComponent();
		}

		private void button1_Click(object sender, System.EventArgs e) {
			// cause an exception to happen
			int x = 0;
			int y = 0 / x;
		}

	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions