Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2007-03-22
7
Medium Priority
?
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 86

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 86

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 86

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 86

Accepted Solution

by:
Mike Tomlinson earned 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

636 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