single application instance

G

Guest

Hi,

I've read several threads about 1 single instance of application and I tried
them but without sucess.
Does anyone have a good example for making the main Form as single instance
application ?

thanks a lot,
PM
 
S

Sakharam Phapale

Hi Maileen,

Try this one.

Public Sub Main()
Dim intAppInstances As Integer
intAppInstances = UBound(Diagnostics.Process.GetProcessesByName
(Diagnostics.Process.GetCurrentProcess.ProcessName))
If intAppInstances > 0 Then
MsgBox("Only one instance can be run at a time.")
Exit Sub
End If
End Sub

Regards
Sakharam Phapale
 
C

C-Services Holland b.v.

Sakharam said:
Hi Maileen,

Try this one.

Public Sub Main()
Dim intAppInstances As Integer
intAppInstances = UBound(Diagnostics.Process.GetProcessesByName
(Diagnostics.Process.GetCurrentProcess.ProcessName))
If intAppInstances > 0 Then
MsgBox("Only one instance can be run at a time.")
Exit Sub
End If
End Sub

Regards
Sakharam Phapale

Shouldn't
If intAppInstances > 0 Then
be
If intAppInstances > 1 Then

Otherwise you can't even run it once since the program checking it is
itself one of the instances.
 
H

Herfried K. Wagner [MVP]

Sakharam Phapale said:
Try this one.

Public Sub Main()
Dim intAppInstances As Integer
intAppInstances = UBound(Diagnostics.Process.GetProcessesByName
(Diagnostics.Process.GetCurrentProcess.ProcessName))
If intAppInstances > 0 Then

The process name is not necessarily unique.
 
M

Maileen

thanks a lot,
I just change it a little bit in order to make it more suitable.
but it helped me a lot.
 
C

Cor Ligthert

Maileen,

Why I point you on the method from Tom is that that prevents that programs
which have the same names as yours are not blocked.

Otherwise is this more than enough
\\\
Private mut As New Threading.Mutex(True, "myProgram", mutCreated)
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not mutCreated Then Me.Close()
////

I hope this gives more ideas

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top