Click here to Skip to main content
15,849,785 members
Articles / Programming Languages / Visual Basic

The Single Instance Class Library

Rate me:
Please Sign up or sign in to vote.
1.25/5 (10 votes)
2 May 2009CPOL 66K   95   14   31
If the user tries to run a second copy of the application, the existing instance should kill itself.
Image 1

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

C#
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

C++
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

VB.NET
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

C#
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)

C#
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)


Written By
Turkey Turkey
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

Comments and Discussions

 
GeneralMy vote of 1 Pin
_Khallaf20-Jun-09 13:57
_Khallaf20-Jun-09 13:57 
QuestionAnything different from .NET provided solution. Pin
PaPaSEK18-Jun-09 18:45
PaPaSEK18-Jun-09 18:45 
AnswerRe: Anything different from .NET provided solution. Pin
_Khallaf20-Jun-09 13:55
_Khallaf20-Jun-09 13:55 
Generalpoor article sorry Pin
cinamon14-Jun-09 12:06
cinamon14-Jun-09 12:06 
GeneralRe: poor article sorry Pin
emarti23-Aug-09 22:42
emarti23-Aug-09 22:42 
GeneralMy vote of 1 Pin
rmyott12-Jun-09 9:02
rmyott12-Jun-09 9:02 
GeneralThis does not work in virtual environment such as under Citrix Pin
rmyott12-Jun-09 9:00
rmyott12-Jun-09 9:00 
GeneralMy vote of 1 Pin
Izzet Kerem Kusmezer11-Jun-09 5:17
Izzet Kerem Kusmezer11-Jun-09 5:17 
GeneralMy vote of 1 Pin
Tomas Rapkauskas5-May-09 5:43
Tomas Rapkauskas5-May-09 5:43 
GeneralMy vote of 1 Pin
Johann Gerell3-May-09 22:22
Johann Gerell3-May-09 22:22 
GeneralRe: My vote of 1 Pin
emarti4-May-09 13:24
emarti4-May-09 13:24 
General[Message Deleted] Pin
Priyank Bolia3-May-09 10:58
Priyank Bolia3-May-09 10:58 
GeneralRe: My vote of 1 Pin
emarti4-May-09 13:04
emarti4-May-09 13:04 
GeneralMy vote of 1 Pin
eitherxor2-May-09 15:12
eitherxor2-May-09 15:12 
GeneralRe: My vote of 1 Pin
emarti2-May-09 16:23
emarti2-May-09 16:23 
GeneralRe: My vote of 1 Pin
jpmik3-May-09 5:48
jpmik3-May-09 5:48 
GeneralMust agree with Ruchit..., Pin
eitherxor2-May-09 15:12
eitherxor2-May-09 15:12 
General[My vote of 1] Here's a better and Accurate way to do this.. PinPopular
Ruchit S.2-May-09 6:34
Ruchit S.2-May-09 6:34 
GeneralRe: [My vote of 1] Here's a better and Accurate way to do this.. Pin
emarti2-May-09 7:50
emarti2-May-09 7:50 
GeneralRe: [My vote of 1] Here's a better and Accurate way to do this.. Pin
Ruchit S.2-May-09 7:54
Ruchit S.2-May-09 7:54 
GeneralRe: [My vote of 1] Here's a better and Accurate way to do this.. Pin
emarti2-May-09 8:14
emarti2-May-09 8:14 
GeneralRe: [My vote of 1] Here's a better and Accurate way to do this.. Pin
Shevchenko72-May-09 12:44
Shevchenko72-May-09 12:44 
emarti wrote:
Here we do not discuss the best solution.


In fact, we do Smile | :)
GeneralRe: [My vote of 1] Here's a better and Accurate way to do this.. Pin
emarti2-May-09 16:27
emarti2-May-09 16:27 
GeneralRe: [My vote of 1] Here's a better and Accurate way to do this.. Pin
Izzet Kerem Kusmezer11-Jun-09 5:26
Izzet Kerem Kusmezer11-Jun-09 5:26 
QuestionSend message to running instance Pin
h4n52-May-09 5:01
h4n52-May-09 5:01 

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

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