none
How to make the message in my code to appear? RRS feed

  • Question

  • Hi all,

    I have this code and it works fine but the message doesn't appear when I try to run another instance of my application

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            If PrevInstance() Then
                MessageBox.Show("The App Is Already Running")
                Me.Close()
                Exit Sub
            End If
        End Sub
    
        Function PrevInstance() As Boolean
            If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
                Return True
            Else
                Return False
            End If
        End Function
    End Class


    • Edited by Max45-1 Wednesday, January 1, 2020 2:02 PM
    Wednesday, January 1, 2020 1:57 PM

Answers

  • Hi,

    Sorry , but the code not working . The message still not appear

    How are you testing it?

    I am also able to use your posted example as is.

    I did notice if you start the app.exe stand alone and then run the app in visual studio to test it does not work.

    Must be two instances of the .exe or two instances running in the VS IDE.

    Tell us exactly how to reproduce the "not working."

    PS ie GetCurrentProcess returns the name app.exe for the stand alone running .exe but returns app.vshost for the process name when running in Visual Studion.



    • Edited by tommytwotrain Thursday, January 2, 2020 1:16 PM
    • Marked as answer by Max45-1 Thursday, January 2, 2020 7:07 PM
    Thursday, January 2, 2020 1:14 PM
  • Hi,

    Sorry , but the code not working . The message still not appear

    Hi

    Try this:

    1, compile the application - take note if it is Debug or Release mode.

    2. Exit the Visual Studio environment completely

    3. If Debug in step 1 then

                        navigate to the Debug folder in file explorer

                        find the .exe file of your application

                        double click to run (first instance)

                        verify application running correctly

                         double click .exe file again (second instance)

                         you should get your messagebox this time

                         every time you run after initial first instance should messagebox

    4. If Release in step 1 then

                        navigate to the Release folder in file explorer

                        repeat as per step 3 above but in the Release folder

    *

    Post back here with the results of these steps.

                   


    Regards Les, Livingston, Scotland

    • Marked as answer by Max45-1 Thursday, January 2, 2020 7:06 PM
    Thursday, January 2, 2020 1:25 PM

All replies

  • Hi

    If you just want to prevent multiple instances, then check out this option.

    Running your exact code does result in the expected messagebox


    Regards Les, Livingston, Scotland


    • Edited by leshay Wednesday, January 1, 2020 2:24 PM
    Wednesday, January 1, 2020 2:17 PM
  • Hello,

    This works for me although I did a refactor so the test is not limited to any one form.

    First select project properties, select Application tab, press the button "View Application Events" which opens a code window. Replace its contents with

    Namespace My
        Partial Friend Class MyApplication
            Public ReadOnly Property AlreadyRunning() As Boolean
                Get
                    Return UBound(Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName)) > 0
                End Get
            End Property
        End Class
    End Namespace

    Form code

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            If My.Application.AlreadyRunning Then
                MessageBox.Show("The App Is Already Running")
            End If
        End Sub
    End Class

    If I run the app from windows explorer twice, the second time the above message appears.

    Another option would be under the application tab of the project properties is to check "Make single instance application" so when a second try to run the app happens it does not start but goes to the first instance of the app.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Wednesday, January 1, 2020 2:20 PM
    Moderator
  • Hi,

    Have you solved this problem now?

    I think the above replies given by Leshay and Karen can provide you with a solution, have you tried them?

    If so, hope you can close this thread by marking the reply as answer as this will help others looking for the same or similar issues down the road.

    Also, you can try my method as follows based on Leshay's reply:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim pro As Process
            For Each pro In Process.GetProcesses
                If pro.ProcessName = Process.GetCurrentProcess.ProcessName Then
                    If pro.Id <> Process.GetCurrentProcess.Id Then
                        MessageBox.Show("The App Is Already Running")
                        Me.Close()
                        Exit Sub
                    End If
                End If
            Next
        End Sub

    Hope it be helpful.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 2, 2020 8:54 AM
    Moderator
  • Hi,

    Sorry , but the code not working . The message still not appear

    Thursday, January 2, 2020 10:12 AM
  • Another usual way is with a Mutex :

    Public mutex As System.Threading.Mutex

    Dim bPrevInstance As Boolean
    mutex = New System.Threading.Mutex(True, "MyMutex", bPrevInstance)
    If (bPrevInstance = False) Then
        MessageBox.Show("The App Is Already Running")
        Application.Exit()
    End If

    Thursday, January 2, 2020 11:30 AM
  • Hi,

    Sorry , but the code not working . The message still not appear

    How are you testing it?

    I am also able to use your posted example as is.

    I did notice if you start the app.exe stand alone and then run the app in visual studio to test it does not work.

    Must be two instances of the .exe or two instances running in the VS IDE.

    Tell us exactly how to reproduce the "not working."

    PS ie GetCurrentProcess returns the name app.exe for the stand alone running .exe but returns app.vshost for the process name when running in Visual Studion.



    • Edited by tommytwotrain Thursday, January 2, 2020 1:16 PM
    • Marked as answer by Max45-1 Thursday, January 2, 2020 7:07 PM
    Thursday, January 2, 2020 1:14 PM
  • Hi,

    Sorry , but the code not working . The message still not appear

    Hi

    Try this:

    1, compile the application - take note if it is Debug or Release mode.

    2. Exit the Visual Studio environment completely

    3. If Debug in step 1 then

                        navigate to the Debug folder in file explorer

                        find the .exe file of your application

                        double click to run (first instance)

                        verify application running correctly

                         double click .exe file again (second instance)

                         you should get your messagebox this time

                         every time you run after initial first instance should messagebox

    4. If Release in step 1 then

                        navigate to the Release folder in file explorer

                        repeat as per step 3 above but in the Release folder

    *

    Post back here with the results of these steps.

                   


    Regards Les, Livingston, Scotland

    • Marked as answer by Max45-1 Thursday, January 2, 2020 7:06 PM
    Thursday, January 2, 2020 1:25 PM
  • No matter who's reply you try the code only works outside of Visual Studio. Place this in form load along with any reply here to test their code.

    If Debugger.IsAttached Then
    MessageBox.Show("Code supplied for multiple instance will not work in the IDE")
    Else
        ' code will work 
    End If


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, January 2, 2020 4:46 PM
    Moderator
  • Thanks a lot. It worked perfectly. 

    • Edited by Max45-1 Thursday, January 2, 2020 7:06 PM
    Thursday, January 2, 2020 6:55 PM