Multiple instance of the same program

F

fniles

I am using VBNET 2008.
I am using a "command line argument" to run 2 different instances of
the program, say 1 instance to connect to program A, and another
instance to connect to program B. So, for 1 instance of the program,
in the program shortcut I append the letter "1", and for the B
instance I append the letter "2". For ex:
C:\program files\test\EServer\EServer.exe 1
C:\program files\test\EServer\EServer.exe 2

But, I do not want to allow running 2 copies of the same instance of
the program, ie I do not want to allow EServer.exe 1 to have 2 copies
running.

Is it possible to do that ?
Thank you.

I do not want to allow running 2 copies of my program

'This is how I get the command line arguments:
For Each s As String In My.Application.CommandLineArgs
sArgument = s
Next
If sArgument <> "" Then
iProcess = sArgument
Else
iProcess = 1
End If

'If I run these codes, I won't be able to run "C:\program files\test
\EServer\EServer.exe 1" and
"C:\program files\test\EServer\EServer.exe 2".

If
Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length
MsgBox("Another instance of " &
Process.GetCurrentProcess.ProcessName & " is already running !")
End
End If
 
A

Armin Zingler

Am 08.07.2010 23:06, schrieb fniles:
I am using VBNET 2008.
I am using a "command line argument" to run 2 different instances of
the program, say 1 instance to connect to program A, and another
instance to connect to program B. So, for 1 instance of the program,
in the program shortcut I append the letter "1", and for the B
instance I append the letter "2". For ex:
C:\program files\test\EServer\EServer.exe 1
C:\program files\test\EServer\EServer.exe 2

But, I do not want to allow running 2 copies of the same instance of
the program, ie I do not want to allow EServer.exe 1 to have 2 copies
running.

Is it possible to do that ?
Thank you.

I do not want to allow running 2 copies of my program

'This is how I get the command line arguments:
For Each s As String In My.Application.CommandLineArgs
sArgument = s
Next
If sArgument <> "" Then
iProcess = sArgument
Else
iProcess = 1
End If

'If I run these codes, I won't be able to run "C:\program files\test
\EServer\EServer.exe 1" and
"C:\program files\test\EServer\EServer.exe 2".

If
Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length
MsgBox("Another instance of " &
Process.GetCurrentProcess.ProcessName & " is already running !")
End
End If

In Sub Main:

dim creatednew as boolean
Dim mux = New Threading.Mutex( _
True, "{B994C532-FB4A-4b26-A40B-9D145156E71F}" & iProcess, createdNew)

Try
If createdNew Then

'**** main code goes here ****

else
'optional message
msgbox "only one instance allowed"
'or console output as 'CommandLineArgs' seems to be
'available to a console application only.
end if
Finally
mux.ReleaseMutex()
End Try

I use a per-application GUID. In this case appending 'iProcess'.

Two hints:
- Enable Option Strict
- You can declare it "Sub Main(args as string())", so you don't have
to call My.Application.CommandLineArgs but just access the array.
 
O

Onur Güzel

I am using VBNET 2008.
I am using a "command line argument" to run 2 different instances of
the program, say 1 instance to connect to program A, and another
instance to connect to program B. So, for 1 instance of the program,
in the program shortcut I append the letter "1", and for the B
instance I append the letter "2". For ex:
C:\program files\test\EServer\EServer.exe 1
C:\program files\test\EServer\EServer.exe 2

But, I do not want to allow running 2 copies of the same instance of
the program, ie I do not want to allow EServer.exe 1 to have 2 copies
running.

Is it possible to do that ?
Thank you.

I do not want to allow running 2 copies of my program

'This is how I get the command line arguments:
For Each s As String In My.Application.CommandLineArgs
sArgument = s
Next
If sArgument <> "" Then
iProcess = sArgument
Else
iProcess = 1
End If

'If I run these codes, I won't be able to run "C:\program files\test
\EServer\EServer.exe 1" and
"C:\program files\test\EServer\EServer.exe 2".

If
Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length>1 Then

MsgBox("Another instance of " &
Process.GetCurrentProcess.ProcessName & " is already running !")
End
End If

Another option besides "Mutex", in project properties, make sure "Make
Single Instance" is checked as like in the screens:

http://www.codeproject.com/KB/vb/vb2005ga/ss1.gif
http://vbcity.com/cfs-filesystemfil...3_5F00_BlogImages/ConfigureSingleInstance.jpg

HTH,

Onur Güzel
 

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