article

Prevent more than one instance of any application

Email
Submitted on: 1/4/2015 12:08:00 AM
By: Nhilesh Baua (from psc cd)  
Level: Intermediate
User Rating: By 2 Users
Compatibility: C#
Views: 2244
 
     Purpose of this application is to have only one running instance of any c# application. Uses "using System.Diagnostics" name space

 
				
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
using DEAdmin.Classes;
using System.Diagnostics;
namespace DEAdmin
{
		
	/// 
	/// Summary description for Form1.
	/// 
	public class frmmain : System.Windows.Forms.Form
	{
		private System.ComponentModel.Container components = null;
		public frmmain()
		{
				//
			// Required for Windows Form Designer support
			//	
			InitializeComponent();
				//
			// TODO: Add any constructor code after InitializeComponent call
			//	
		}
 	
		/// 
		/// Clean up any resources being used.
		/// 		 
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		#region Windows Form Designer generated code
			/// 
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// 	
		private void InitializeComponent()
		{
				// 
			// frmmain
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(792, 473);
			this.IsMdiContainer = true;
			this.Menu = this.mainMenu1;
			this.Name = "frmmain";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Dhalia Enterprise Main Screen";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.Resize += new System.EventHandler(this.frmmain_Resize);
			this.Load += new System.EventHandler(this.Form1_Load);
		}
		#endregion
		/// 
		/// The main entry point for the application.
		/// 
		[STAThread]
		static void Main() 
		{
			Application.Run(new frmmain());
		}
		private void dataGrid1_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)
		{
			
		}
		private void Form1_Load(object sender, System.EventArgs e)
		{
			if (RunningInstance()!=null)
			{
				this.Dispose();
			}
		}
		public static Process RunningInstance() 
 
		{ 
			Process current = Process.GetCurrentProcess(); 
			Process[] processes = Process.GetProcessesByName (current.ProcessName); 
			//Loop through the running processes in with the same name 
			foreach (Process process in processes) 
			{ 
				if (process.Id != current.Id) 
				{ 
					if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName) 
					{ 
						return process; 
					} 
				} 
			} 
			return null; 
		} 
		


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.