![]() |
Languages »
VB.NET »
Samples
Intermediate
VB.NET Code Package: Program Already RunningBy George B GilbertTest at startup whether another instance of an application is already running, with three options for handling the situation. |
VB, Windows, .NET, Visual Studio, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

Demo project.

Option that does not allow a second copy of the application to run.

Option that lets the user run multiple copies of the application.
Add this code to a VB.NET project when you want to test if another instance of the application is already running. You can either end the program, give the user the option to run the extra copy of the application, or make the previous copy of the application the active window.
The code is available in a package file for Double Text users, as well as in a text file for non-Double Text users.
I set up the Program Already Running library because I found that determining whether or not an instance of an application is already running is something that I want in just about every project. In addition, the nature of each application dictates whether or not it's OK to allow multiple copies of the application to be running at the same time. The PAR library is a perfect example of simple code that I use often, but, code I don't want to spend any significant time on to add to each new project. Hence, the source file is set up to give me the ability to generate in seconds, the code I need, that is tailored to each new project.
The Program Already Running (PAR) library consists of one source file (.DbC) and an overview (.DbO). The code in the source file is a protected sub that overrides a form's OnHandleCreated sub. Placing the code in this event allows the decision about what to do (when another copy of the application is already running) to be made before the form is loaded.
The source text is set up with selectable blocks of code so that each new copy of the sub can be customized during duplication to function as needed for your current project. In addition to including or not including Region statements, you can configure each new copy of the code to either:
I also embedded two required responses, for the name of the developer and the name of the application, to be displayed in the message box text. There are two other markers (filler and date) that do not trigger dialog boxes during duplication.
The PAR source file generates fully commented code that is ready to compile.
Option Strict On.
The library is ready to go. Repeat the code per the following instructions whenever you need a ready to compile copy. (If you don't like the way I've set up the source file, change it in the edit window.)
Each time you need a copy of the sub procedure, follow these steps in Double Text. (These steps apply as written only if you do not change how the source file is set up.)






The ready to compile sub procedure is now on the clipboard. If you would like to review the code, press F4.
OnHandleCreated sub.
If you configured the sub to make the previous copy of the application the active window, you must add the supporting API Declares to the form. Follow these steps in Double Text.


All of the above instructions for using the library (less the images) are in the library overview. Click on the Float button in the Double Text main window whenever you need a refresher. (If you change the source file, don't forget to reflect those changes in the overview.)
Download and unzip the demo file. Open the solution in Visual Studio. The demo project is coded to make the copy of the application that is already running the active window.
Download and unzip the text file. I repeated the three possible versions of the OnHandleCreated sub so there are three copies of the sub in separate text files ... PAR End Program, PAR Option To Run, and PAR Make Active Window. When you need this code, use whichever tool you prefer to make a copy of the appropriate text file, and then make any needed modifications. You might want to find and change the following either before or after you paste the code into your project in the startup form.
This version of the sub ends the program after alerting the user that there is a copy of the application already running.
#Region " ... OnHandleCreated "
Protected Overrides Sub OnHandleCreated(ByVal e As System.EventArgs)
'------------------------------------------------------------
' Date Developer Code Change
' ---------- -------------------- --------------------------
' 12/10/2005 G Gilbert Original code
'------------------------------------------------------------
'------------------------------------------------------------
' Get all of the active processes for the application
'------------------------------------------------------------
Dim CurrentProcesses() As Process
CurrentProcesses = Process.GetProcessesByName _
(Process.GetCurrentProcess.ProcessName)
'------------------------------------------------------------
' If there is no previous copy
' of the application running, bail
'------------------------------------------------------------
If CurrentProcesses.GetUpperBound(0) <= 0 Then
Exit Sub
End If
'------------------------------------------------------------
' Alert the user that a copy of the application
' is already running and then end
' this new instance with no recourse
'------------------------------------------------------------
Dim XMsg As String = "A copy of Your Application Name" & _
ControlChars.CrLf & _
"is already running"
MessageBox.Show(XMsg, _
"Copy Running", _
MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
End
End Sub
#End Region
This version of the sub alerts the user that there is a copy of the application already running and gives the user the option to also run the copy just starting.
#Region " ... OnHandleCreated "
Protected Overrides Sub OnHandleCreated(ByVal e As System.EventArgs)
'------------------------------------------------------------
' Date Developer Code Change
' ---------- -------------------- --------------------------
' 12/10/2005 G Gilbert Original code
'------------------------------------------------------------
'------------------------------------------------------------
' Get all of the active processes for the application
'------------------------------------------------------------
Dim CurrentProcesses() As Process
CurrentProcesses = Process.GetProcessesByName _
(Process.GetCurrentProcess.ProcessName)
'------------------------------------------------------------
' If there is no previous copy
' of the application running, bail
'------------------------------------------------------------
If CurrentProcesses.GetUpperBound(0) <= 0 Then
Exit Sub
End If
'------------------------------------------------------------
' Give the user the option to run
' multiple copies of this application
'------------------------------------------------------------
Dim XMsg As String = "A copy of Your Application Name" & _
ControlChars.CrLf & _
"is already running" & _
ControlChars.CrLf & ControlChars.CrLf & _
"Continue?"
If MessageBox.Show(XMsg, _
"Copy Running", _
MessageBoxButtons.YesNo, _
MessageBoxIcon.Question) = DialogResult.No Then
End
End If
End Sub
#End Region
This version of the sub makes the copy of the application that's already running the active window and then ends. Note that the Declare statements must be moved to the form's declarations section.
Declare Function OpenIcon Lib "user32" (ByVal hwnd As Long) As Long
Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
#Region " ... OnHandleCreated "
Protected Overrides Sub OnHandleCreated(ByVal e As System.EventArgs)
'------------------------------------------------------------
' Date Developer Code Change
' ---------- -------------------- --------------------------
' 12/10/2005 G Gilbert Original code
'------------------------------------------------------------
'------------------------------------------------------------
' Get all of the active processes for the application
'------------------------------------------------------------
Dim CurrentProcesses() As Process
CurrentProcesses = Process.GetProcessesByName _
(Process.GetCurrentProcess.ProcessName)
'------------------------------------------------------------
' If there is no previous copy
' of the application running, bail
'------------------------------------------------------------
If CurrentProcesses.GetUpperBound(0) <= 0 Then
Exit Sub
End If
'------------------------------------------------------------
' Make the prior instance of the application
' the active window and terminate this
' new instance
'------------------------------------------------------------
Dim i As Integer
Dim Result As Long
Dim ProcessHandle As Long
For i = 0 To CurrentProcesses.GetUpperBound(0)
ProcessHandle = CurrentProcesses(i).MainWindowHandle.ToInt32
If ProcessHandle <> 0 Then
'** Restore and activate the copy already running
Result = OpenIcon(ProcessHandle)
Result = SetForegroundWindow(ProcessHandle)
End If
Next
End
End Sub
#End Region
The End statement is used to terminate the new instance of an application instead of Application.Exit(). The latter statement allows the startup form to load before the program is terminated. This causes a disturbing flash on the screen. With the End statement, the form is not loaded and, thereby, the flash is avoided.
Determining whether or not another instance of an application is running is a function that has been done to death on code exchange sites. Usually, the code will make the previous copy of the application the active window when there is a copy already running. That is an option, however, making the application the active window may not be appropriate for all projects. In addition, other versions of this code I have run across during my research tend to be unnecessarily lengthy. I prefer my versions because they're concise, and, the configurable Double Text library affords me a full range of options for how each new copy of the code I generate will handle the "already running" situation.
You may use the Double Text library and all code offered in this article any way you choose without restriction.
Under no circumstances, and under no legal theory, tort, contract, or otherwise, will George Gilbert (hereafter referred to as "software author") or his licensors, be liable to the user of the Double Text library and all code offered in this article (hereafter referred to collectively as "article code"), for any damages, including any lost profits, lost data, or other indirect, special, incidental or consequential damages, arising out of the use or inability to use the article code, and data or information supplied, even if the software author, his licensors or authorized dealer have been advised of the possibility of such damages, or for any claim by any other party.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 9 Jan 2006 Editor: Smitha Vijayan |
Copyright 2006 by George B Gilbert Everything else Copyright © CodeProject, 1999-2009 Web15 | Advertise on the Code Project |