Solved

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

Posted on 2007-03-22
7
229 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert an ASPX page into PDF 7 31
Get Top Visible Row of Datagridview 6 32
Exit the loop 4 42
Run a batch file when a Crystal Report is opened 5 27
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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

815 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

8 Experts available now in Live!

Get 1:1 Help Now