Click here to Skip to main content
Licence CPOL
First Posted 2 May 2009
Views 25,475
Downloads 29
Bookmarked 14 times

The Single Instance Class Library

By | 2 May 2009 | Article
If the user tries to run a second copy of the application, the existing instance should kill itself.

Introduction

Sometimes, we do not want to run the same copy of the application. Here in this case, you can use the single instance class library. This library includes a static function. This function detects a second copy of the application that is already running. If the user tries to run a second copy of the application, the existing instance should kill itself.

Using the Code

Single Instance Class Library C# Code

using System.Diagnostics;

namespace SingleInstance
{
    public class SingleInstance
    {
        /// <summary>
        /// Detect a second copy of the application that is already running.
        /// If the user tries to run a second copy of the application, 
        /// the existing instance should kill itself. 
        /// </summary>
        public static void SingleInst()
        {
            if (Process.GetProcessesByName
		(Process.GetCurrentProcess().ProcessName).Length > 1)
                Process.GetCurrentProcess().Kill();
        }
    }
}

Single Instance Class Library MC++ Code

namespace SingleInstance
{
    public __gc class SingleInstance
    {
        /// <summary>
        /// Detect a second copy of the application that is already running.
        /// if the user tries to run a second copy of the application, 
        /// the existing instance should kill itself. 
        /// </summary>
        public: static void __gc* SingleInst()
        {
            if (Process::GetProcessesByName
		(Process::GetCurrentProcess()->ProcessName)->Length > 1)
            {
                Process::GetCurrentProcess()->Kill();
            }
        }
    };
}

Single Instance Class Library VB Code

Imports System
Imports System.Diagnostics

Namespace SingleInstance
    Public Class SingleInstance
        ' <summary>
        ' Detect a second copy of the application that is already running.
        ' if the user tries to run a second copy of the application, 
        ' the existing instance should kill itself. 
        ' </summary>
        Public Shared Sub SingleInst()
            If (Process.GetProcessesByName_
		(Process.GetCurrentProcess.ProcessName).Length > 1) Then
                Process.GetCurrentProcess.Kill
            End If
        End Sub

    End Class
End Namespace

Console Application Sample Code

using System;

namespace SingleInstConsole
{
    class Program
    {
        static void Main()
        {
            // Single Instance Application
            SingleInstance.SingleInstance.SingleInst();

            Console.WriteLine("Merhaba " + Environment.UserName);
            Console.ReadLine();
        }
    }
}

Windows Form Application Sample (Program.cs)

using System;
using System.Windows.Forms;

namespace SingleInstWindowsFormsApp
{
    static class Program
    {
        /// <summary />
        /// The main entry point for the application.
        /// </summary />
        [STAThread]
        static void Main()
        {
            // Single Instance Application
            SingleInstance.SingleInstance.SingleInst();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

History

  • 2nd May, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

emarti

Other
Dentist (Dental Office)
Turkey Turkey

Member

Follow on Twitter Follow on Twitter
My name is Murat Özdemir.
 
I'm a Turkish Dentist and living in Eskişehir (Turkey).
 
Software programming is one of my hobbies since 1990 and I like to deal with 1 and 0.
 
Do not forget to brush your teeth for your health. Smile | :)
 
Visit my homepage at http://emarti.sf.net

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
GeneralMy vote of 1 Pinmember_Khallaf12:57 20 Jun '09  
QuestionAnything different from .NET provided solution. PinmemberPaPaSEK17:45 18 Jun '09  
AnswerRe: Anything different from .NET provided solution. Pinmember_Khallaf12:55 20 Jun '09  
Generalpoor article sorry Pinmembercinamon11:06 14 Jun '09  
GeneralRe: poor article sorry Pinmemberemarti21:42 23 Aug '09  
GeneralMy vote of 1 Pinmemberrmyott8:02 12 Jun '09  
GeneralThis does not work in virtual environment such as under Citrix Pinmemberrmyott8:00 12 Jun '09  
GeneralMy vote of 1 PinmemberIzzet Kerem Kusmezer4:17 11 Jun '09  
GeneralMy vote of 1 PinmemberTomas Rapkauskas4:43 5 May '09  
GeneralMy vote of 1 PinmemberJohann Gerell21:22 3 May '09  
GeneralRe: My vote of 1 Pinmemberemarti12:24 4 May '09  
General[Message Deleted] PinmemberTalking Clipboard9:58 3 May '09  
GeneralRe: My vote of 1 Pinmemberemarti12:04 4 May '09  
GeneralMy vote of 1 PinmemberJohn_Wesley14:12 2 May '09  
GeneralRe: My vote of 1 Pinmemberemarti15:23 2 May '09  
GeneralRe: My vote of 1 PinmemberJean-Paul Mikkers4:48 3 May '09  
GeneralMust agree with Ruchit..., PinmemberJohn_Wesley14:12 2 May '09  
General[My vote of 1] Here's a better and Accurate way to do this.. PinPopularmemberRuchit S.5:34 2 May '09  
GeneralRe: [My vote of 1] Here's a better and Accurate way to do this.. Pinmemberemarti6:50 2 May '09  
GeneralRe: [My vote of 1] Here's a better and Accurate way to do this.. PinmemberRuchit S.6:54 2 May '09  
GeneralRe: [My vote of 1] Here's a better and Accurate way to do this.. Pinmemberemarti7:14 2 May '09  
GeneralRe: [My vote of 1] Here's a better and Accurate way to do this.. PinmemberShevchenko711:44 2 May '09  
GeneralRe: [My vote of 1] Here's a better and Accurate way to do this.. Pinmemberemarti15:27 2 May '09  
GeneralRe: [My vote of 1] Here's a better and Accurate way to do this.. PinmemberIzzet Kerem Kusmezer4:26 11 Jun '09  
QuestionSend message to running instance Pinmemberh4n54:01 2 May '09  

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
Web02 | 2.5.120517.1 | Last Updated 2 May 2009
Article Copyright 2009 by emarti
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid