Results 1 to 10 of 10

Thread: VB.NET: Single Instance Application

  1. #1

    Thread Starter
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    VB.NET: Single Instance Application

    In VB 6, we had a very friendly App object which would give us lot of information regarding the current application. If one wanted to check if there is already an instance of this application running it was just two lines of code. In VB.NET 2002/2003 we do not have any such object so to check whether we already have an instance of the application running we need to do some extra work.

    Here is a function which checks if the instance of the application is already running. If returns a Process object which contains information about the currently running instance or a null value(if the application is not running).
    VB Code:
    1. 'Function : checkInstance
    2.     'Input Parameter : None
    3.     'Return Value : Returns the Process if the exe
    4.     'is already running or else returns nothing
    5.     Public Function checkInstance() As Process
    6.         Dim cProcess As Process = process.GetCurrentProcess()
    7.         Dim aProcesses() As Process = process.GetProcessesByName(cProcess.ProcessName)
    8.         'loop through all the processes that are currently running on the
    9.         'system that have the same name
    10.         For Each process As Process In aProcesses
    11.             'Ignore the currently running process
    12.             If process.Id <> cProcess.Id Then
    13.                 'Check if the process is running using the same EXE as this one
    14.                 If [Assembly].GetExecutingAssembly().Location = cProcess.MainModule.FileName Then
    15.                     'if so return to the calling function with the instance of the process
    16.                     Return process
    17.                 End If
    18.             End If
    19.         Next
    20.         'if nothing was found then this is the only instance, so return null
    21.         Return Nothing
    22.     End Function
    We just have to call this function at the entry point of our application. If the return value is Null then this is the first instance and if it returns a process then we already have an instance of the application running on the system. Ideally a project will be started from Sub Main, so I would put the code in the Sub Main procedure.
    VB Code:
    1. Public Sub main()
    2.         Dim tempProcess As Process
    3.         tempProcess = checkInstance()
    4.         If tempProcess Is Nothing Then
    5.             Application.Run(New Form1)
    6.         Else
    7.             MessageBox.Show("Application is already running")
    8.         End If
    9.     End Sub
    In case you want to set focus to the currently running instance then you can get the MainWindowHandle of the already running instance like this
    VB Code:
    1. tempProcess.MainWindowHandle
    And then use SetForeGroundWindow Api to activate the already running instance.

    Don't forget to Import System.Diagnostics namespace.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  2. #2

    Thread Starter
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: VB.NET: Single Instance Application

    Another way of doing the same would be to use Threading. Associate a static Mutex with the thread that starts the application and then check for the same. Here is a sample
    VB Code:
    1. Imports System.Threading
    2.  
    3. Module Module1
    4.     Private siMutex As Mutex
    5.     Public Sub main()
    6.         siMutex = New Mutex(False, "aUniqueName")
    7.         If (Not siMutex.WaitOne(0, False)) Then
    8.             MessageBox.Show("Application is Running")
    9.         Else
    10.             MessageBox.Show("Application Not Running")
    11.             siMutex.ReleaseMutex()
    12.         End If
    13.     End Sub
    14. End Module
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VB.NET: Single Instance Application

    You mention 2002/2003.

    Does the same code work for/in 2005?

  4. #4

    Thread Starter
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: VB.NET: Single Instance Application

    In VB 2005 checking making your application single instance has been made as easy as clicking a checkbox. And it even allows you to set the Focus to the already running instance without writing a single line of code.

    Open Project Properties, and Select Application. Under Windows Application Framework properties check the "Make Single Instance Application" check box. Now if you build the project, your application will automatically become a single instance application.

    And if you want to bring the already running instance to Foreground thats also easy. Trap the My.Application.StartupNextInstance event and set e.BringToForeground to true.
    VB Code:
    1. Namespace My
    2.     Partial Friend Class MyApplication
    3.         Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, _
    4. ByVal e As _
    5. Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
    6.             e.BringToForeground = True
    7.         End Sub
    8.     End Class
    9. End Namespace
    Isn't it easy.

    I have started loving VB 2005.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VB.NET: Single Instance Application

    Fine job!

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: VB.NET: Single Instance Application

    Quote Originally Posted by Shuja Ali
    In VB 2005 checking making your application single instance has been made as easy as clicking a checkbox. And it even allows you to set the Focus to the already running instance without writing a single line of code.

    Open Project Properties, and Select Application. Under Windows Application Framework properties check the "Make Single Instance Application" check box. Now if you build the project, your application will automatically become a single instance application.

    And if you want to bring the already running instance to Foreground thats also easy. Trap the My.Application.StartupNextInstance event and set e.BringToForeground to true.
    VB Code:
    1. Namespace My
    2.     Partial Friend Class MyApplication
    3.         Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, _
    4. ByVal e As _
    5. Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
    6.             e.BringToForeground = True
    7.         End Sub
    8.     End Class
    9. End Namespace
    Isn't it easy.

    I have started loving VB 2005.
    Without using Microsoft.VisualBasic can it be done also in C# Express 2005?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  7. #7

    Thread Starter
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: VB.NET: Single Instance Application

    Quote Originally Posted by dee-u
    Without using Microsoft.VisualBasic can it be done also in C# Express 2005?
    As far as I know C# Express 2005 does not give you that option. You still have to write code to do that. The above methods can be used in C# Express 2005 too.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  8. #8
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: VB.NET: Single Instance Application

    Quote Originally Posted by dee-u
    Without using Microsoft.VisualBasic can it be done also in C# Express 2005?
    No. See my codebank submission: http://vbforums.com/showthread.php?t=397006
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  9. #9
    New Member
    Join Date
    May 2012
    Posts
    2

    Re: VB.NET: Single Instance Application

    Just oneline code only

    Dim _process() As Process _process = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName)

    and check the _process.Length

    thats all

    if you need full source code

    http://net-informations.com/vbprj/fr...e-instance.htm

    algor.

  10. #10
    New Member
    Join Date
    Sep 2012
    Posts
    1

    Re: VB.NET: Single Instance Application

    I take it that Process.GetProcessByName retrieves ALL matching processes that are running on the computer - regardless of which user the process is running under. If we only wanted to test to see if the is process is already running for the Current User - then how might we modify this (ie. the app is single instance per user, but want to permit someone to Switch User & run a new instance under a different account).

    I'm assuming that comparing that any of the found processes .SessionID = current process .SessionID would do the trick, and permit the program to continue to run if they're not equal.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width