Click to See Complete Forum and Search --> : But for one man, One launch is enough...


visual fzz
December 22nd, 1999, 04:59 AM
Hi everybody

I want my application to be started only 1 time.
If the user launch again the executable, it activates the application already opened and do not open a second application...

How can I do that??
Specification of the project?
Test if this process already exists in the machine
at the launch of the app??

Fzz

Chris Eastwood
December 22nd, 1999, 05:47 AM
You can use the '.PrevInstance' property of the 'App' object at runtime to see if an instance of your program is already running.

eg.


If App.PrevInstance then
End ' or Unload me etc
End If




If you want a more detailed explanation and to see how to copy data between a newly started instance and an existing instance, take a look at :

http://codeguru.developer.com/vb/articles/1804.shtml

Chris Eastwood

CodeGuru - the website for developers
http://codeguru.developer.com/vb

Ruth Glushkin
December 22nd, 1999, 06:46 AM
Add the following code to your MainForm_Initialize() subroutine

' Checks if the Application is already running
If (App.PrevInstance = True) Then End

Good Luck!

LarryLunchBucket
December 22nd, 1999, 01:56 PM
for some reason that doesn;t work

Chris Eastwood
December 22nd, 1999, 02:34 PM
I'd be interested to see how you're testing it - the App.PrevInstance will only be true at run-time when you already have an instance of your program running (you can't test it from the IDE because VB only allows one copy of a project open at a time).


Chris Eastwood

CodeGuru - the website for developers
http://codeguru.developer.com/vb

Ruth Glushkin
December 23rd, 1999, 12:38 AM
Component that uses App.PrevInstance can cause problems in Win NT

A computer running Windows NT can support multiple desktops. When you're using a component designed to work with distributed COM, this can result in the following scenario:

· A client program in a user desktop requests one of the objects the component provides. Because the component is physically located on the same machine, the component is started in the user desktop.
· Subsequently, a client program on another computer uses distributed COM to request one of the objects the component provides. A second instance of the component is started, in a system desktop.

There are now two instances of the component running on the same NT computer, in different desktops.

This scenario is not a problem, unless the author of the component has placed a test for App.PrevInstance in the startup code for the component, to prevent multiple copies of the component from running on the same computer. In this case, the remote object creation will fail.

So try to check the App.PrevInstance in Win 95/98.

Good Luck!

Noog
December 27th, 1999, 12:59 PM
I need your help. I knew I could check if the app was already running using App.PrevInstance, but my problem is slightly different. I want to know how to pass command line arguments from the latest opened instance to the previous one (i.e. if my program opens files, I want all files to be opened in the same window). Have I to use DDE or something similar?
Thanks in advance

AndyK
December 27th, 1999, 11:44 PM
Place this code into form_load

If App.PrevInstance = true then
End
End If

AndyK
December 27th, 1999, 11:48 PM
That code goes into Form_Load()
and to test it, ss Chris already said, you need to make an .exe and try to run it twice

Ruth Glushkin
December 28th, 1999, 12:51 AM
You have some different ways to do it. The easiest one is to save all the arguments into a new ASCII text file, which after that your second application can read. If you don't like it, try API Function
Send_Message(), but don't forget about syncronization, if your first application is still not running, and
you already trying to send the message to it from the second application, you'll have a problem. File is
also good when you want to share data between Windows and DOS applications. Anyway, good luck!

Noog
December 28th, 1999, 08:53 AM
Really thanks! But if I create a text file, the existing instance has to repeatedly check if this file exists or has been updated, hasn't it?
Er... I'm a total learner about API, can you at least tell me how works Send_Message()?

Sorry if I bother you again, and thanks in advance!

Ruth Glushkin
December 29th, 1999, 12:41 AM
I am sending you the link of good enough example of SendMessage() function:

http://www.vbgreatone.8m.com/VB/s/sendmessage.htm

Good Luck!

fab12
December 29th, 1999, 04:46 AM
Well that is definitely a very interesting question!

Best Regards ;-)

Noog
December 29th, 1999, 02:48 PM
Really thanks!!! The API guide I was looking for!!!