Check if app is already running

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Passalacqua

    #1

    Check if app is already running

    In VB.NET, how do you check to see if an instance of your application is
    already running?

    Michael Passalacqua
    Portland Community College
    CIS Faculty

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • meh

    #2
    Re: Check if app is already running

    If you make a public function similar to this

    Public Shared Function PrevInstance() As Process

    Dim c As Process = Process.GetCurr entProcess()

    Dim p As Process

    For Each p In Process.GetProc essesByName(c.P rocessName)

    If p.Id <> c.Id Then

    If p.MainModule.Fi leName = c.MainModule.Fi leName Then

    Return p

    End If

    End If

    Next p

    Return Nothing

    End Function

    And then call it from say the Form load event or a sub main

    If Not PrevInstance() Is Nothing Then

    MsgBox("Myapp is already running !", MsgBoxStyle.Inf ormation, "Myapp")

    End

    ' Else

    ' Exit.

    End If

    Hope this helps...

    meh

    "Michael Passalacqua" <mpassala@pcc.e du> wrote in message news:e1CqniOmDH A.424@TK2MSFTNG P10.phx.gbl...[color=blue]
    > In VB.NET, how do you check to see if an instance of your application is
    > already running?
    >
    > Michael Passalacqua
    > Portland Community College
    > CIS Faculty
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it![/color]

    Comment

    • Armin Zingler

      #3
      Re: Check if app is already running

      "Michael Passalacqua" <mpassala@pcc.e du> schrieb[color=blue]
      > In VB.NET, how do you check to see if an instance of your application
      > is already running?[/color]

      It's been asked and answered two hours ago. Subject: Is app already running?


      --
      Armin

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Check if app is already running

        * Michael Passalacqua <mpassala@pcc.e du> scripsit:[color=blue]
        > In VB.NET, how do you check to see if an instance of your application is
        > already running?[/color]

        <http://www.google.com/groups?selm=blg rft%24c51nq%244 %40ID-208219.news.uni-berlin.de>

        --
        Herfried K. Wagner
        MVP ยท VB Classic, VB.NET
        <http://www.mvps.org/dotnet>

        Comment

        • rgsiii
          New Member
          • Apr 2006
          • 1

          #5
          better mousetrap solution

          Unfortunately, even checking file paths is not fullproof.

          If you really want to prevent multiple instances, regardless of if the myApp.exe is in a different folder, or even if they rename the file, then you have to take a different approach.

          One of the easier solutions is to set up a OS level named Mutex and check that when the app starts.

          The following c# code demonstrates (which can be easily translated into vb.net if desired):

          Code:
          Mutex myMutex = null;
          
          //ensure that the app is not already running!
          //this method will prevent duplicates, even if the user has
          //changed the exe name.  This method is more reliable than just
          //checking for process name in the process list!
          try
          {
          	Console.WriteLine("Checking to see if app is already running...");
          	reeMutex = Mutex.OpenExisting( "myMutexName" );
          	//if we were able to open the mutex
          	//that means that it already existed
          	//which means that the app is already running
          	//which means we need to abaondon ship
          
          	Console.WriteLine( Process.GetCurrentProcess().MainModule.FileName + " is already running! Only a single MyApp can run at a time. This instance will shut down." );
          	return ;
          
          }
          catch( WaitHandleCannotBeOpenedException )
          {
          	//This is the FIRST instantiation, 
          	//which means that this is the first run of the app
          	//which means we can keep on running
          	//so go ahead and create the mutex
          	Console.WriteLine( "Mutex does not exist, creating..." );
          	bool mutexWasCreated = false;
          	reeMutex = new System.Threading.Mutex( true, mutexName, out mutexWasCreated );
          
          }
          catch( UnauthorizedAccessException )
          {
          	//if there was a security access error
          	//that means that it already existed (even though we could't open it)
          	//which means that the appis already running
          	//which means we need to abaondon ship
          
          	Console.WriteLine( Process.GetCurrentProcess().MainModule.FileName + " is already running! Only a single MyApp can run at a time. This instance will shut down." );
          	return;
          
          }
          Enjoy!
          Robert

          Comment

          Working...