Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to prevent duplicate apps running 2

Status
Not open for further replies.

DotNetNewbie

Programmer
Mar 3, 2004
344
GB
Hi everyone,

Just a quick, and probably easy question. I would like to add some code to my app so that it can only be opened once, if a user double-clicks the shortcut it just sets the focus to the already open application.

However i dont have a clue on how to do that so any help is most welcome.

Thanks

D
 
This should be possible by use of the Singleton pattern.

Craig
 
I use this code in my apps:

Imports System.Diagnostics.Process

Public Module GetInstanceCount()

Public Sub Main()
Dim Proc() as Process

Dim ModuleName, ProcName as String
ModuleName = Process.GetCurrentProcess.MainModule.ModuleName
ProcName = System.IO.Path.GetFileNameWithoutExtension(ModuleName)

Proc = Process.GetProcessByname(ProcName)
If Proc.Length > 1 then Appliction.Exit

End Sub

End Module

**This code came out of "Visual Basic .Net Programmers Cookbook" by Matthew MacDonald

Shane
 
Try this out; this will detect duplicate instance and Activates the previous instance.

Copy the following code in a Module.

Code:
'Declarations of Windows API functions.
    Declare Function OpenIcon Lib "user32" (ByVal hwnd As Long) As Long
    Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

    Sub ActivatePrevInstance(ByVal argStrAppToFind As String)

        Dim PrevHndl As Long
        Dim result As Long

        'Variable to hold individual Process.
        Dim objProcess As New Process()

        'Collection of all the Processes running on local machine
        Dim objProcesses() As Process

        'Get all processes into the collection
        objProcesses = Process.GetProcesses()

        For Each objProcess In objProcesses
            'Check and exit if we have SMS running already
            If UCase(objProcess.MainWindowTitle) = UCase(argStrAppToFind) Then
                MessageBox.Show(argStrAppToFind & " is already running.", "System", MessageBoxButtons.OK, MessageBoxIcon.Information)
                PrevHndl = objProcess.MainWindowHandle.ToInt32()
                Exit For
            End If
        Next

        'If no previous instance found exit the application.
        If PrevHndl = 0 Then Exit Sub

        'If previous instance found.
        result = OpenIcon(PrevHndl) 'Restore the program.
        result = SetForegroundWindow(PrevHndl) 'Activate the application.

        'End the current instance of the application.
        End

End Sub

Then, use the following code in Form Load event of your Main (starting) Form.

Code:
'On Form Load.
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Prevent multiple instances of the application.
        If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
            ActivatePrevInstance("YourMainFormName")
        End If
End Sub





Email: pankajmsm@yahoo.com
 
and a combination of the two gives magic, a star for you both, one each offcourse

Christiaan Baes
Belgium
"What a wonderfull world" - Louis armstrong
 
I've tried the code from PankajBanga and everything works except the first instance of the app doesn't come to the foreground. Any thoughts or suggestions? Platform is Windows XP Pro SP1, .NET Framework 1.1, SP1.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top