Groups | Blog | Home
all groups > dotnet general > july 2004 >

dotnet general : Avoid running multiple program instance ????



serge calderara
7/4/2004 11:56:02 PM
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
dave
7/5/2004 2:22:04 AM
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


[quoted text, click to view]
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
[quoted text, click to view]
defining a mutex object, or simply checking the instance
value but nothing goes.
[quoted text, click to view]
Rakesh Rajan
7/5/2004 9:49:02 AM
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.
--
Rakesh Rajan


[quoted text, click to view]
Mr. B
7/6/2004 12:30:07 AM
[quoted text, click to view]

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,

AddThis Social Bookmark Button