?
Solved

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

Posted on 2007-03-22
7
Medium Priority
?
254 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month9 days, 4 hours left to enroll

764 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