Prevent Multiple Instances of a .NET Windows Application

This tip describes how to avoid loading a second instance of an application when the user already has one instance running. It also sets the focus to the first instance of the .NET Windows application when you attempt to start a second instance of the same application.

This code is applicable to the .NET 1.0 and 1.1 Framework applications (Visual Studio.NET 2002 uses .NET 1.0 framework and Visual Studio.NET 2003 uses .NET 1.1 Framework).

''Add this code in the form_load event.. (the form which loaded and shownas the first form)''This works well with an MDI form or a non-MDI formPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles MyBase.Load	IfUBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then		''Send opening form's TEXT property as a parameter to the function"ActivatePrevInstance"		''This works well with an MDI form or a non-MDI form		''It is advised that you give a Unique name to your Form so that it doenot conflict with other applications	      ActivatePrevInstance(TEXT_PROPERTY_OF_OPENING_FORM)	End IfEnd Sub----------------------------------------------Add these following declarations in the form code or in a COMMON moduleif you have one.''Declarations of Windows API functionsDeclare Function OpenIcon Lib "user32" (ByVal hwnd As Long) As LongDeclare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) AsLongSub ActivatePrevInstance(ByVal argStrAppToFind As String)        Dim PrevHndl As Long        Dim result As Long        Dim objProcess As New Process 'Variable to hold individual Process        Dim objProcesses() As Process 'Collection of all the Processesrunning on local machine        objProcesses = Process.GetProcesses() ''Get all processes into thecollection        For Each objProcess In objProcesses            ''Check and exit if we have SMS running already            If UCase(objProcess.MainWindowTitle) = UCase(argStrAppToFind)Then                MsgBox("Another instance of " & argStrAppToFind & " isalready running on this machine. You cannot run TWO instances at a time.Please use the other instance.")                PrevHndl = objProcess.MainWindowHandle.ToInt32()                Exit For            End If        Next        If PrevHndl = 0 Then Exit Sub 'if No previous instance found exitthe application.        ''If found        result = OpenIcon(PrevHndl) 'Restore the program.        result = SetForegroundWindow(PrevHndl) 'Activate the application.        End 'End the current instance of the application.End Sub

As a result of this code, the first instance of the program is given focus and the second instance is closed. If the first instance of the application was minimized, it will be restored to a normal window automatically.

Though this code is written in VB.NET, it can easily be converted into C# or JScript easily on similar grounds.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may