Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

How to find out whether there's already running an instance of your application when a user wants to start it?

I would like to allow only one instance running on a machine.

qrt
  • *Gurus*
Posted

Straight from the help:

 

In Visual Basic 6.0, the PrevInstance property of the App object was used to determine if a previous instance of an application was running. There is no equivalent for this property in Visual Basic .NET; however, the following code can be used to test for a previous instance:

 

' Visual Basic .NET
Function PrevInstance() As Boolean
  If Ubound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
     Return True
  Else
     Return False
  End If
End Function

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted
thanx divil. Any suggestions where the best placement would be to call this check (in the constructor of the start up form or something?), so I can cancel the start up of a second instance?
qrt
  • Leaders
Posted

Check out [msdn]System.Threading.Mutex[/msdn]. Which will allow you to do something like this, which prevents any renaming workarounds.

      Dim objLock As New System.Threading.Mutex(False, "MyAppLock")
       Dim blnIsRunning As Boolean = Not mtxLock.WaitOne(0, False)

--tim

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...