Results 1 to 11 of 11

Thread: One instance of app

  1. #1

    Thread Starter
    Lively Member chongo 2002's Avatar
    Join Date
    Apr 2000
    Posts
    106

    One instance of app

    How do i keep a user from opening more than one of the same vb application? I want only one to be able to be run at a time, unlike msword that can run multiple times.
    Last edited by chongo 2002; Sep 14th, 2009 at 02:48 PM.

  2. #2
    Hyperactive Member PITBULLCJR's Avatar
    Join Date
    Nov 1999
    Location
    New York
    Posts
    408
    If App.PrevInstance = True Then
    Unload Me
    End If
    Sincerely,
    Chris


    Email: [email protected]
    AIM: KnightsOfTheMoon
    WebPage: http://kom.wicre.com
    ----------------
    VB6 Professional
    Abit ST6-RAID
    1000 MHZ
    512 MB PC133 Ram
    Nvidia GeForce 2 Ultra 64 MB
    Maxtor 81.9 Gig
    Win 98 SE

  3. #3
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    I got this code on this forum somewhere. It is better than using the App.Previnstance / unload method because it actually jumps to the currently running instance of ur project.
    Regards
    Stuart
    VB Code:
    1. Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    2. (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    3. Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _
    4. ByVal wCmd As Long) As Long
    5. Declare Function OpenIcon Lib "user32" (ByVal hwnd As Long) As Long
    6. Declare Function SetForegroundWindow Lib "user32" (ByVal _
    7. hwnd As Long) As Long
    8.          
    9. Public Const GW_HWNDPREV = 3
    10.  
    11. Public Sub ShowPrevInstance()
    12.     On Error Resume Next
    13.     Dim OldTitle As String
    14.     Dim ll_WindowHandle As Long
    15.     'saving the current title in OldTitle variable and changing the application title
    16.     OldTitle = App.Title
    17.     App.Title = "New App - This App Will Be Closed"
    18.     'finding the previous instance. if you are using VB 5.0 change "ThunderRT6Main" to "ThunderRT5Main"
    19.     ll_WindowHandle = FindWindow("ThunderRT6Main", OldTitle)
    20.     'if there is no old instances of your application - exit.
    21.     If ll_WindowHandle = 0 Then Exit Sub
    22.     'Find the window we need to restore
    23.     ll_WindowHandle = GetWindow(ll_WindowHandle, GW_HWNDPREV)
    24.     'Now restore it
    25.     Call OpenIcon(ll_WindowHandle)
    26.     'And Bring it to the foreground
    27.     Call SetForegroundWindow(ll_WindowHandle)
    28.     Unload Me
    29.     Set [Your Form Name Here] = Nothing
    30. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  4. #4

    Thread Starter
    Lively Member chongo 2002's Avatar
    Join Date
    Apr 2000
    Posts
    106
    thanks

  5. #5

    Thread Starter
    Lively Member chongo 2002's Avatar
    Join Date
    Apr 2000
    Posts
    106
    On the last line does the title go inside [] or did you just put those there as markers? show me an example just to make sure i understand.

  6. #6
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    Just insert the name of the form that loads at start up in ur app
    So, if it is form1 do...

    Set Form1 = Nothing

    You dont even need this line in all cases but does no harm
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  7. #7

    Thread Starter
    Lively Member chongo 2002's Avatar
    Join Date
    Apr 2000
    Posts
    106
    got ya

  8. #8

    Thread Starter
    Lively Member chongo 2002's Avatar
    Join Date
    Apr 2000
    Posts
    106
    Ok the code works in the case that it willnot allow another application to open but it does not popup the running application do you know why?

  9. #9
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi again
    It works but i hope that you actually called the routine...
    In ur Sub Main or Form Load or wherever your project starts u need to call the routine
    VB Code:
    1. If App.PrevInstance Then ShowPrevInstance
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  10. #10

    Thread Starter
    Lively Member chongo 2002's Avatar
    Join Date
    Apr 2000
    Posts
    106
    Thanks that code works great

  11. #11
    Addicted Member Sibby's Avatar
    Join Date
    Feb 2001
    Location
    Milwaukee, WI *The United States of America*
    Posts
    144
    I don't know about VB6, but VB5's class window name is "ThunderRT5Form", NOT "ThunderRT5Main".

    NOTE: You can find the classname of any window using "Find window" option in SPY++ (bundled with VC++). It's very easy and it's helped me out alot recently.

    ::Sibby::
    If you can think it....you can code it....

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