Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Running one Instance of EXE

Running one Instance of EXE

Scheduled Pinned Locked Moved Visual Basic
csharphelptutorial
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pradipta
    wrote on last edited by
    #1

    I have a VB.net Application ,whenever i click on the EXE a new instance of the application runs .But i want one instance to run at a time .That means if the exe is already running by clicking on it it should show the message that it is already running. Similar case for loading forms.suppose i have a button ,clicking on it a form is comes up ,if i click on the button several instance of the same form is running ,how to get rid of this that means there must be one instance of a form at a time. Please help me its urgent

    G C G 3 Replies Last reply
    0
    • P pradipta

      I have a VB.net Application ,whenever i click on the EXE a new instance of the application runs .But i want one instance to run at a time .That means if the exe is already running by clicking on it it should show the message that it is already running. Similar case for loading forms.suppose i have a button ,clicking on it a form is comes up ,if i click on the button several instance of the same form is running ,how to get rid of this that means there must be one instance of a form at a time. Please help me its urgent

      G Offline
      G Offline
      Giles
      wrote on last edited by
      #2

      Use a Mutex - so you are going to have to look at the Win32 API. Have a look at CreateMutex - to make a named Mutex. You can only create a Mutex once across processes, so the if a second instance launches and tries to create it, there will be an error, which can be caught. So you would put this routine in the start up code.


      Shameless Plug - Distributed Database Transactions in .NET using COM+

      C 1 Reply Last reply
      0
      • P pradipta

        I have a VB.net Application ,whenever i click on the EXE a new instance of the application runs .But i want one instance to run at a time .That means if the exe is already running by clicking on it it should show the message that it is already running. Similar case for loading forms.suppose i have a button ,clicking on it a form is comes up ,if i click on the button several instance of the same form is running ,how to get rid of this that means there must be one instance of a form at a time. Please help me its urgent

        C Offline
        C Offline
        CBoland
        wrote on last edited by
        #3

        Interesting questions! For the single app instance, try using a Mutex: Sub Main() Dim M1 As System.Threading.Mutex Dim createdNew As Boolean = False M1 = New System.Threading.Mutex(True, "MyUniqueMutexName", createdNew) If createdNew = False Then MsgBox("Other process is running") Return End If Application.Run(New Form1()) End Sub For single form intance, this is a bit of a hack, but it works. The problem with maintaining a member-level ref to a form occurs after the form has been closed. At this point the form is disposed, but we have no way of determining the disposed state by examining the ref (that I know of). My hack catches an ObjectDisposedException as a way of determining the disposed state. You could also subclass Form, override Dispose(bool) and provide an IsDisposed property. ' Member-level reference Private _Form2 As Form2 ... Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If _Form2 Is Nothing Then _Form2 = New Form2() Try _Form2.Show() _Form2.BringToFront() Catch ex As ObjectDisposedException _Form2 = New Form2() _Form2.Show() End Try End Sub

        P 1 Reply Last reply
        0
        • G Giles

          Use a Mutex - so you are going to have to look at the Win32 API. Have a look at CreateMutex - to make a named Mutex. You can only create a Mutex once across processes, so the if a second instance launches and tries to create it, there will be an error, which can be caught. So you would put this routine in the start up code.


          Shameless Plug - Distributed Database Transactions in .NET using COM+

          C Offline
          C Offline
          CBoland
          wrote on last edited by
          #4

          See my reply to the original message. There is a System.Threading.Mutex class available in the Fx.

          G 1 Reply Last reply
          0
          • P pradipta

            I have a VB.net Application ,whenever i click on the EXE a new instance of the application runs .But i want one instance to run at a time .That means if the exe is already running by clicking on it it should show the message that it is already running. Similar case for loading forms.suppose i have a button ,clicking on it a form is comes up ,if i click on the button several instance of the same form is running ,how to get rid of this that means there must be one instance of a form at a time. Please help me its urgent

            G Offline
            G Offline
            GrindAZ
            wrote on last edited by
            #5

            This will work for detecting an instance of the application already running. If (UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0) Then '//End The App Or Whatever You Want To Do Here End If

            1 Reply Last reply
            0
            • C CBoland

              See my reply to the original message. There is a System.Threading.Mutex class available in the Fx.

              G Offline
              G Offline
              Giles
              wrote on last edited by
              #6

              Oops, did not see that, I thought he was talking ordinary VB for a moment. Anyway Mutex would seem the way to go, and its much easier in .NET. :)


              Shameless Plug - Distributed Database Transactions in .NET using COM+

              1 Reply Last reply
              0
              • C CBoland

                Interesting questions! For the single app instance, try using a Mutex: Sub Main() Dim M1 As System.Threading.Mutex Dim createdNew As Boolean = False M1 = New System.Threading.Mutex(True, "MyUniqueMutexName", createdNew) If createdNew = False Then MsgBox("Other process is running") Return End If Application.Run(New Form1()) End Sub For single form intance, this is a bit of a hack, but it works. The problem with maintaining a member-level ref to a form occurs after the form has been closed. At this point the form is disposed, but we have no way of determining the disposed state by examining the ref (that I know of). My hack catches an ObjectDisposedException as a way of determining the disposed state. You could also subclass Form, override Dispose(bool) and provide an IsDisposed property. ' Member-level reference Private _Form2 As Form2 ... Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If _Form2 Is Nothing Then _Form2 = New Form2() Try _Form2.Show() _Form2.BringToFront() Catch ex As ObjectDisposedException _Form2 = New Form2() _Form2.Show() End Try End Sub

                P Offline
                P Offline
                pradipta
                wrote on last edited by
                #7

                thanx CBoland Its great and working as per my expectation. Thanx a lot Pradipta

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups