Multiple Instances

hoverctafter

Member
Joined
May 15, 2005
Messages
9
Programming Experience
10+
I am trying to develop an application that you can open documents with the current application (ie. using the openwith). These are just simple files that contain instructions for the program to know which server to join and a few other parameters. I am retrieving the file name with the following sub which is my start up module


Public Sub Main(ByVal Args() As String) 'Add the startup call back

If Args.Length <> 0 Then

Dim i As Integer = 0

Do While i <> Environment.GetCommandLineArgs.GetUpperBound(0)

ArgFileName = ArgFileName & Args(i) & " "

i = i + 1

Loop

ArgFileName = Mid(ArgFileName, 1, ArgFileName.Length - 1)

End If

Application.Run(New frmMain)

End Sub

My question is how can I prevent another instance of my application from opening when I already have one openened. I would like if you select open with and the application is already opened, then it just send the file name to the already opened application and add it to the programs files list.
Basically I want it to work like photoshop.. If you click a picture to edit with photoshop and you dont have photoshop open, then it opens it up. If photostop is open, then it opens the picture into photoshop.

TIA for any help
Hovercrafter
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
Hi,
You may check the process by the process name and determine if create a new instance.

VB.NET:
Dim myProcess As Processes = Process.GetProcessesByName("Your program name goes here")
 
	If MyProcesses.Length <> 1 Then
MessageBox.Show("There is another instance of this Application !")
	 '//
Else
 
Application.Run([size=2][color=#0000ff]New[/color][/size][size=2] frmMain)
[/size]

Cheers ;)

edit: there is another aproach as well ... you could check this out http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=726
 
Last edited:

hoverctafter

Member
Joined
May 15, 2005
Messages
9
Programming Experience
10+
Thanks for the quick reply. Actually I had gotten that far, the probelm was with passing the file name to the already opened instance. Doing some massive googling I found an article with sample code that helped me out very much:

http://www.codeproject.com/vb/net/singleinstance.asp#xx496768xx

That is the article and the sample works great if anyone else would need to implement this in an app they are writing.
Happy coding
Hovercrafter
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
i've got a nifty function on my puter @ home that checks the processes to see if there's an instance of the new one already running i'll post it when i get home in a couple of hours
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
sorry for the late reply here's the function that will check to see if a previous instance of your application is already running:

VB.NET:
Dim blnPrevApp As Boolean = PrevInstance()
If blnPrevApp = True Then Me.Close()

 Function PrevInstance() As Boolean
  If Ubound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
	Return True
  Else
	Return False
  End If
 End Function
also since this get's your application name directly from the environment you can simply copy this function and add it to other applications without needing to modify it and/or you can change your applications name and not need to modify this function
 
Top Bottom