Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

The Single Instance Class Library

1.25/5 (10 votes)
2 May 2009CPOL 68.1K   96  
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)