Click here to Skip to main content
15,888,286 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 66.4K   96   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 12:57
_Khallaf20-Jun-09 12:57 
QuestionAnything different from .NET provided solution. Pin
PaPaSEK18-Jun-09 17:45
PaPaSEK18-Jun-09 17:45 
AnswerRe: Anything different from .NET provided solution. Pin
_Khallaf20-Jun-09 12:55
_Khallaf20-Jun-09 12:55 
Generalpoor article sorry Pin
cinamon14-Jun-09 11:06
cinamon14-Jun-09 11:06 
GeneralRe: poor article sorry Pin
emarti23-Aug-09 21:42
emarti23-Aug-09 21:42 
GeneralMy vote of 1 Pin
rmyott12-Jun-09 8:02
rmyott12-Jun-09 8:02 
GeneralThis does not work in virtual environment such as under Citrix Pin
rmyott12-Jun-09 8:00
rmyott12-Jun-09 8:00 
GeneralMy vote of 1 Pin
Izzet Kerem Kusmezer11-Jun-09 4:17
Izzet Kerem Kusmezer11-Jun-09 4:17 
GeneralMy vote of 1 Pin
Tomas Rapkauskas5-May-09 4:43
Tomas Rapkauskas5-May-09 4:43 
GeneralMy vote of 1 Pin
Johann Gerell3-May-09 21:22
Johann Gerell3-May-09 21:22 
GeneralRe: My vote of 1 Pin
emarti4-May-09 12:24
emarti4-May-09 12:24 
General[Message Deleted] Pin
Priyank Bolia3-May-09 9:58
Priyank Bolia3-May-09 9:58 
GeneralRe: My vote of 1 Pin
emarti4-May-09 12:04
emarti4-May-09 12:04 
GeneralMy vote of 1 Pin
eitherxor2-May-09 14:12
eitherxor2-May-09 14:12 
GeneralRe: My vote of 1 Pin
emarti2-May-09 15:23
emarti2-May-09 15:23 
GeneralRe: My vote of 1 Pin
jpmik3-May-09 4:48
jpmik3-May-09 4:48 
GeneralMust agree with Ruchit..., Pin
eitherxor2-May-09 14:12
eitherxor2-May-09 14:12 
General[My vote of 1] Here's a better and Accurate way to do this.. PinPopular
Ruchit S.2-May-09 5:34
Ruchit S.2-May-09 5:34 
GeneralRe: [My vote of 1] Here's a better and Accurate way to do this.. Pin
emarti2-May-09 6:50
emarti2-May-09 6:50 
GeneralRe: [My vote of 1] Here's a better and Accurate way to do this.. Pin
Ruchit S.2-May-09 6:54
Ruchit S.2-May-09 6:54 
GeneralRe: [My vote of 1] Here's a better and Accurate way to do this.. Pin
emarti2-May-09 7:14
emarti2-May-09 7:14 
GeneralRe: [My vote of 1] Here's a better and Accurate way to do this.. Pin
Shevchenko72-May-09 11:44
Shevchenko72-May-09 11:44 
GeneralRe: [My vote of 1] Here's a better and Accurate way to do this.. Pin
emarti2-May-09 15:27
emarti2-May-09 15:27 
Your idea for the best? Smile | :)

Divinum est sedare dolorem

GeneralRe: [My vote of 1] Here's a better and Accurate way to do this.. Pin
Izzet Kerem Kusmezer11-Jun-09 4:26
Izzet Kerem Kusmezer11-Jun-09 4:26 
QuestionSend message to running instance Pin
h4n52-May-09 4:01
h4n52-May-09 4: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.