Solved

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

Posted on 2007-03-22
7
205 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Help with uploading excel file 2 31
WPF issue with Trigger 2 66
Handling arrays 14 33
Get list of word ducuments in a folder 10 14
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

14 Experts available now in Live!

Get 1:1 Help Now