Click here to Skip to main content
Licence 
First Posted 7 Mar 2004
Views 24,283
Bookmarked 17 times

Restrict your application from creating a new instance

By | 7 Mar 2004 | Article
This sample code uses the System.Diagnostics namespace to restrict the application from creating an instance more than once.

Introduction

After spending some time studying the C#, I started to develop some components and applications so i thought before i could jump into articles relating to user controls i could contribute this code snippet that retricts the application from creating multiple instances.

The following code uses the System.Diagnostics namespace to identify the application process name. So you just have to create new windows application project and just replace code in the main function in the form with the code provided below.

static void Main()
Int32 _isProcessRunning;
_isProcessRunning = System.Diagnostics.Process.GetProcessesByName(<BR>  System.Diagnostics.Process.GetCurrentProcess().ProcessName).Length;
if(_isProcessRunning != 1)
{
  MessageBox.Show("Already an Instance running");
}
else
{
  Application.Run(new Form1());
}

Test your application by executing your application exe from the \bin\Debug folder.

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

About the Author

rajeshvasan

Web Developer

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralNothing new... PinmemberDavid M. Kean10:53 8 Mar '04  
GeneralRe: Nothing new... Pinmemberrajeshvasan17:36 8 Mar '04  
GeneralRe: Nothing new... Pinmembervitaminchik8:27 18 Mar '04  
GeneralRe: Nothing new... Pinmemberhemanext23:34 1 Jun '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 8 Mar 2004
Article Copyright 2004 by rajeshvasan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid