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

 
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 
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 
AnswerRe: Send message to running instance Pin
emarti2-May-09 7:05
emarti2-May-09 7:05 
GeneralRe: Send message to running instance Pin
h4n52-May-09 7:38
h4n52-May-09 7:38 
GeneralRe: Send message to running instance Pin
emarti2-May-09 10:02
emarti2-May-09 10:02 
If you want to run application with different parameters, not need single instance class library. According to me, you must try using parameters only. In fact, I do not understand you what to say to me Smile | :)

Divinum est sedare dolorem

GeneralRe: Send message to running instance Pin
h4n53-May-09 2:02
h4n53-May-09 2:02 
GeneralRe: Send message to running instance Pin
Rick York4-May-09 7:11
mveRick York4-May-09 7:11 
GeneralRe: Send message to running instance Pin
Izzet Kerem Kusmezer11-Jun-09 4:19
Izzet Kerem Kusmezer11-Jun-09 4:19 

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.