CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Nov 1999
    Location
    France
    Posts
    29

    But for one man, One launch is enough...

    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


  2. #2
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: But for one man, One launch is enough...

    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

  3. #3
    Join Date
    Dec 1999
    Location
    Israel
    Posts
    101

    Re: But for one man, One launch is enough...

    Add the following code to your MainForm_Initialize() subroutine

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

    Good Luck!


  4. #4
    Join Date
    Dec 1999
    Posts
    8

    Re: But for one man, One launch is enough...

    for some reason that doesn;t work


  5. #5
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: But for one man, One launch is enough...

    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

  6. #6
    Join Date
    Dec 1999
    Location
    Israel
    Posts
    101

    Re: But for one man, One launch is enough...

    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!


  7. #7
    Join Date
    Dec 1999
    Location
    Italy
    Posts
    6

    Re: But for one man, One launch is enough...

    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


  8. #8
    Join Date
    Aug 1999
    Location
    US, Florida
    Posts
    817

    Re: But for one man, One launch is enough...

    Place this code into form_load

    If App.PrevInstance = true then
    End
    End If





  9. #9
    Join Date
    Aug 1999
    Location
    US, Florida
    Posts
    817

    Re: But for one man, One launch is enough...

    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


  10. #10
    Join Date
    Dec 1999
    Location
    Israel
    Posts
    101

    Re: But for one man, One launch is enough...

    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!


  11. #11
    Join Date
    Dec 1999
    Location
    Italy
    Posts
    6

    Re: But for one man, One launch is enough...

    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!



  12. #12
    Join Date
    Dec 1999
    Location
    Israel
    Posts
    101

    Re: But for one man, One launch is enough...

    I am sending you the link of good enough example of SendMessage() function:

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

    Good Luck!


  13. #13
    Join Date
    Nov 1999
    Location
    France
    Posts
    50

    Re: But for one man, One launch is enough...

    Well that is definitely a very interesting question!

    Best Regards ;-)


  14. #14
    Join Date
    Dec 1999
    Location
    Italy
    Posts
    6

    Re: But for one man, One launch is enough...

    Really thanks!!! The API guide I was looking for!!!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured