By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,877 Members | 1,064 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,877 IT Pros & Developers. It's quick & easy.

Check if app is already running

P: n/a
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!
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
meh
If you make a public function similar to this

Public Shared Function PrevInstance() As Process

Dim c As Process = Process.GetCurrentProcess()

Dim p As Process

For Each p In Process.GetProcessesByName(c.ProcessName)

If p.Id <> c.Id Then

If p.MainModule.FileName = c.MainModule.FileName 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.Information, "Myapp")

End

' Else

' Exit.

End If

Hope this helps...

meh

"Michael Passalacqua" <mp******@pcc.edu> wrote in message news:e1*************@TK2MSFTNGP10.phx.gbl...
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!

Nov 20 '05 #2

P: n/a
"Michael Passalacqua" <mp******@pcc.edu> schrieb
In VB.NET, how do you check to see if an instance of your application
is already running?


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

Nov 20 '05 #3

P: n/a
* Michael Passalacqua <mp******@pcc.edu> scripsit:
In VB.NET, how do you check to see if an instance of your application is
already running?


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

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #4

P: 1
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):

Expand|Select|Wrap|Line Numbers
  1. Mutex myMutex = null;
  2.  
  3. //ensure that the app is not already running!
  4. //this method will prevent duplicates, even if the user has
  5. //changed the exe name.  This method is more reliable than just
  6. //checking for process name in the process list!
  7. try
  8. {
  9.     Console.WriteLine("Checking to see if app is already running...");
  10.     reeMutex = Mutex.OpenExisting( "myMutexName" );
  11.     //if we were able to open the mutex
  12.     //that means that it already existed
  13.     //which means that the app is already running
  14.     //which means we need to abaondon ship
  15.  
  16.     Console.WriteLine( Process.GetCurrentProcess().MainModule.FileName + " is already running! Only a single MyApp can run at a time. This instance will shut down." );
  17.     return ;
  18.  
  19. }
  20. catch( WaitHandleCannotBeOpenedException )
  21. {
  22.     //This is the FIRST instantiation, 
  23.     //which means that this is the first run of the app
  24.     //which means we can keep on running
  25.     //so go ahead and create the mutex
  26.     Console.WriteLine( "Mutex does not exist, creating..." );
  27.     bool mutexWasCreated = false;
  28.     reeMutex = new System.Threading.Mutex( true, mutexName, out mutexWasCreated );
  29.  
  30. }
  31. catch( UnauthorizedAccessException )
  32. {
  33.     //if there was a security access error
  34.     //that means that it already existed (even though we could't open it)
  35.     //which means that the appis already running
  36.     //which means we need to abaondon ship
  37.  
  38.     Console.WriteLine( Process.GetCurrentProcess().MainModule.FileName + " is already running! Only a single MyApp can run at a time. This instance will shut down." );
  39.     return;
  40.  
  41. }
  42.  
Enjoy!
Robert
Apr 26 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.