Avoid running multiple program instance ????

G

Guest

Dear all,

I have a vb.net application which start with a sub main procedure.
inside this sub main procedure I create a and instance from an assembly x like as follow:

sub main()
.....

Application.Run(New frmLogin)
end sub

frmLogin is a class inside assembly X

What I need to do, is to avoid launching 2 instances of my program. At the end my main startup programm which contains just the sub main is called NNS.exe wich instantiate a form object from X.dll

I have try many suggesting I get , like the findwindow, defining a mutex object, or simply checking the instance value but nothing goes.

Does anyone as a solution to avoid lunching multiple instance ??

regards
serge
 
D

dave

place this in the form load:

Dim aModuleName As String =
Diagnostics.Process.GetCurrentProcess.MainModule.ModuleName
Dim aProcName As String =
System.IO.Path.GetFileNameWithoutExtension(aModuleName)
If Process.GetProcessesByName(aProcName).Length > 1 Then
Me.Close()
Exit Sub
End If

-----Original Message-----
Dear all,

I have a vb.net application which start with a sub main procedure.
inside this sub main procedure I create a and instance
from an assembly x like as follow:
sub main()
.....

Application.Run(New frmLogin)
end sub

frmLogin is a class inside assembly X

What I need to do, is to avoid launching 2 instances of
my program. At the end my main startup programm which
contains just the sub main is called NNS.exe wich
instantiate a form object from X.dll
I have try many suggesting I get , like the findwindow,
defining a mutex object, or simply checking the instance
value but nothing goes.
 
G

Guest

Hmm...this indeed works. But what happens if i have the same EXE copied into a diff file name and invoke that?
This logic assumes that the original process's assembly and the current processe's assembly has the same name. I wonder what to do in that scenario.
 
M

Mr. B

With Deft Fingers said:
Hmm...this indeed works. But what happens if i have the same EXE copied into a diff file name and invoke that?
This logic assumes that the original process's assembly and the current processe's assembly has the same name. I wonder what to do in that scenario.

Try this...

' Check for Existing Running Application
Dim c As Process = Process.GetCurrentProcess()
Dim p As Process
For Each p In Process.GetProcessesByName(c.ProcessName)
If p.Id <> c.Id Then
msgPrompt = "This Program is already Running!"
msgTitle = "Program Opened"
MsgBox(msgPrompt, vbOKOnly, msgTitle)
' Close 2nd Application Event
Me.Close()
If p.MainModule.FileName = c.MainModule.FileName Then
End If
End If
Next p


Regards,

Bruce
 

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