Solved

limiting multiple running instances of the same vb.net 2003 application??

Posted on 2007-03-22
7
217 Views
Last Modified: 2010-04-23
Experts,
How can I limit the number of the same vb.net programs running to 1.   I developed an application in vb.net 2003, and created a setup project for the application.  The setup puts a short cut on the users desktop.  Every double click of the short cut opens another instance of the same program.  I don't want this to happen.  If for example the user  opens the application and minimizes and forgets that it is already running, I need the short cut to pull up the already running application.  Is something like this possible?
0
Comment
Question by:tentavarious
  • 4
  • 3
7 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18775036
Sure it is...        (it's even easier in VB.Net 2005)

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Const SW_SHOWNORMAL As Integer = 1

    Private Declare Function IsIconic Lib "user32" _
        (ByVal hWnd As IntPtr) As Integer
    Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As IntPtr, _
        ByVal nCmdShow As Integer) As Integer
    Private Declare Function SetForegroundWindow Lib "user32" _
        (ByVal hWnd As IntPtr) As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim appName = System.IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath)
        Dim ps() As Process = Process.GetProcessesByName(appName)
        If ps.Length > 1 Then
            Dim mainHandle As IntPtr = ps(0).MainWindowHandle
            If IsIconic(mainHandle) <> 0 Then
                ShowWindow(mainHandle, SW_SHOWNORMAL)
            End If
            SetForegroundWindow(mainHandle)
            Application.Exit()
        End If
    End Sub

End Class
0
 

Author Comment

by:tentavarious
ID: 18775068
Ok that works,  I also found this code that works,  my problem know is that if the first app is minimized how can i show it in it's normal window state if the short cut is double clicked again?

form load event
  If Not (Me.RunningInstance Is Nothing) Then
         MessageBox.Show("This application is already running")
          duplicate_app = True
           Me.Close()
          Exit Sub
        End If

 Public Shared Function RunningInstance() As Process
        Dim current As Process = process.GetCurrentProcess
        Dim processes As Process() = process.GetProcessesByName(current.ProcessName)
        For Each process As Process In processes
            If Not (process.Id = current.Id) Then
                If Reflection.Assembly.GetExecutingAssembly.Location.Replace("/", "\") = current.MainModule.FileName Then
                    Return process
                End If
            End If
        Next
        Return Nothing
    End Function
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18775090
"my problem know is that if the first app is minimized how can i show it in it's normal window state if the short cut is double clicked again?"

All of that is taken care of in my example above...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:tentavarious
ID: 18775107
It doesnt work, I am using a notifyicon, so that my app only appears in the tray bar.  When I use your method and try to load a second application i just get a quick screen flicker and the original app stays in its minimized state.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18775152
"It doesnt work, I am using a notifyicon"

Well of course it doesn't work then!...an icon in the system tray is NOT the same as a minimized form.

"Waking up" an app from the system tray is significantly trickier in VB.Net 2003 as you must write custom interprocess communication code.  Doing this in VB.Net 2005 is a snap.

Basically you have to send a custom message using Win APIs to the previous app and trap it in the main forms WndProc() method.  You want to go that route?...
0
 

Author Comment

by:tentavarious
ID: 18779430
Nah, as long as two of the same applications are not opening up.  Which code is more efficient mine or yours?  I don't use a lot of win api functions, because I don't know many.  How do you find a list of windows functions, do you just know them from experience with other programming languages?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 18780069
Sorry...I just know them from experience...

If you just want to prevent multiple instances then it can simply down to:

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath)).Length > 1 Then
            Application.Exit()
        End If
    End Sub

End Class
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now