Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2008 > Visual Basic 2008 Essentials
|
Visual Basic 2008 Essentials If you are new to Visual Basic programming with version 2008, this is the place to start your questions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2008 Essentials section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 10th, 2009, 08:05 PM
Registered User
 
Join Date: May 2009
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default Detecting a previous instance of my program

How can I prevent a program from opening a second instance of itself? I would like to maximize the running instance if it exists.
 
Old May 10th, 2009, 08:52 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
Default You could do this...

Just check the processes running and then compare them to the one trying to start up. If it's already listed then kill the one trying to run.

Code:
        Dim appProc() As Process
        Dim strModName As String
        Dim strProcName As String

        strModName = Process.GetCurrentProcess.MainModule.ModuleName
        strProcName = System.IO.Path.GetFileNameWithoutExtension(strModName)

        appProc = Process.GetProcessesByName(strProcName)
         If appProc.Length > 1 Then
            MsgBox("This program is already running. You can't open more than one instance.", MsgBoxStyle.OkOnly, "Error")
            Application.Exit()
        End If
__________________
Jason Hall

Follow me on Twitter @jhall2013

Last edited by alliancejhall; May 10th, 2009 at 08:53 PM.. Reason: Forgot some code!
The Following User Says Thank You to alliancejhall For This Useful Post:
SBSteve (May 10th, 2009)
 
Old May 11th, 2009, 05:01 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

Hi Steve,

I don't know if it's a preferred or better way to Jason's but my C# Programmers Cookbook suggests using a Mutex (http://msdn.microsoft.com/en-us/libr...ing.mutex.aspx) to control if an application can run. This can be useful in other scenarios as multiple programs can use it.

vbnet Code:
Imports System.Threading

Module Module1

    Sub Main()
        Dim ownsMutex As Boolean

        ' try to create a named system mutex
        Using mutex As New Mutex(True, "MutexExample", ownsMutex)
            If ownsMutex Then
                ' got it - run program
                Console.WriteLine("I own the mutex. Press Enter to release")
                Console.ReadLine()
                ' very important to release it once done!!!
                mutex.ReleaseMutex()
            Else
                ' program already running. quit
                Console.WriteLine("App is already running. Exiting...")
            End If

            Console.WriteLine("Press Enter to quit")
            Console.ReadLine()
        End Using
    End Sub

End Module

Phil
 
Old May 11th, 2009, 05:32 PM
Registered User
 
Join Date: May 2009
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default Thanks for the help

Thanks for the answers. alliancejhall's example worked for me as far as detecting the running instance, but I am still having a problem with getting the running instance to restore its window if minimized or hidden.
 
Old May 11th, 2009, 08:16 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
Default How about this...

This is totally untested but it might get you started..

Code:
    Declare Function ShowWindow Lib "user32" (ByVal winHandle As IntPtr, ByVal nCmdShow As Long) As Long

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CheckAppIsRunning()
    End Sub

    Private Sub CheckAppIsRunning(Optional ByVal Arg As String = Nothing)

        Dim appProc() As Process

        Dim strModName As String
        Dim strProcName As String
        strModName = Process.GetCurrentProcess.MainModule.ModuleName
        strProcName = System.IO.Path.GetFileNameWithoutExtension(strModName)

        appProc = Process.GetProcessesByName(strProcName)

        Dim mProcess As Process = Nothing
        For Each p As Process In appProc
            If mProcess Is Nothing Then
                mProcess = p
            Else
                If p.StartTime < mProcess.StartTime Then mProcess = p
            End If
        Next

        Dim winHandle As IntPtr = mProcess.MainWindowHandle

        If appProc.Length > 1 Then
            Call ShowWindow(winHandle, 1)
            MsgBox("This program is already running. You can't open more than one instance.", MsgBoxStyle.OkOnly, "Program Error")
            Application.Exit()
        End If

    End Sub
__________________
Jason Hall

Follow me on Twitter @jhall2013
 
Old May 12th, 2009, 01:09 AM
Registered User
 
Join Date: May 2009
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default Were almost there

Thanks for your help Jason. I tried your code, but the window only appears for a second and then disappears behind other windows. I tried several values for the showWindow() function's second parameter, but never got a window that would stay on top.
By the way, is there a list of the values for the second parameter?

Steve Lyons
 
Old May 12th, 2009, 08:20 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
Default Very sorry...

I should have included that.

MAXIMIZE = 3
RESTORE = 9
NORMAL = 1
SHOW = 5

These are all the constants you would need i would suggest doing something like

Call ShowWindow(winHandle, 1)
Call ShowWindow(winHandle, 5)

or

Call ShowWindow(winHandle, 9)
Call ShowWindow(winHandle, 5)

By combining the calls you are bringing up the window then showing it which activates it.
__________________
Jason Hall

Follow me on Twitter @jhall2013





Similar Threads
Thread Thread Starter Forum Replies Last Post
Detecting a Yahoo Toolbar MosheGolden Javascript How-To 0 November 12th, 2006 04:42 AM
detecting SQL server down geogomez SQL Server 2000 3 August 1st, 2006 08:04 AM
Setup Project: Program not added in Start>Program arif_1947 VS.NET 2002/2003 2 March 31st, 2005 06:40 AM
detecting a form nfontaine Javascript How-To 2 April 20th, 2004 06:53 AM
Detecting Read-Only rgerald Access VBA 6 October 30th, 2003 04:51 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.