Results 1 to 4 of 4

Thread: App.PrevInstance [resolved]

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    App.PrevInstance [resolved]

    OK, I have 2 forms, the main one I want it to show only once, and the other as many times as you run the program, but it always showing either one or the other (in the same instance).

    The problem with the next code is that when I run the program with a command i.e. the frmRestore is showing, the next time I run the program without the command, the main form does not show up, and I want it to because there is no previous instance of the main form.
    If I do it the other way around, then it works. If I close all instances of my program, run it without a command, then run it with a command, then it works.... get it ?
    VB Code:
    1. Private Sub Form_Load()
    2.     If Command <> "" Then
    3.         Load frmRestore
    4.         Unload Me
    5.         Exit Sub
    6.     End If
    7.    
    8.     If App.PrevInstance Then End
    9. End Sub
    Reading what I wrote, it still seems a bit confuzing. I'll try another way...
    So... I have 2 forms, main form and frmRestore, when I run the program more than once, I want the main form to show only once, but the frmRestore more than once, and it should not matter in witch order I run them, main form should always be once...
    Last edited by CVMichael; Jan 4th, 2004 at 02:21 AM.

  2. #2
    Addicted Member imbue's Avatar
    Join Date
    Aug 2002
    Location
    Midwest USA
    Posts
    155
    Could you make two different apps? Then each app would of course have its own App.PrevInstance. If the main form wants to load up and it's already loaded it could just shut down, regardless if the frmRestore is loaded. If you pass it a command it could just shell the frmRestore program.

    I hope I understood your question correctly.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    The program is ~6000 lines, and both forms have a lot of common code, making 2 apps would create a lot of problems.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803
    I fixed the problem using some APIs...

    VB Code:
    1. Private Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString As String) As Integer
    2. Private Declare Function GlobalDeleteAtom Lib "kernel32" (ByVal nAtom As Integer) As Integer
    3. Private Declare Function GlobalFindAtom Lib "kernel32" Alias "GlobalFindAtomA" (ByVal lpString As String) As Integer
    4.  
    5. Private Const MyAtomName As String = "MyApp12345"
    6. Private CurrAtom As Integer
    7.  
    8. Private Sub Form_Load()
    9.     Dim Atom As Integer
    10.  
    11.     If Command <> "" Then
    12.         Load frmRestore
    13.         Exit Sub
    14.     End If
    15.  
    16.     If App.PrevInstance Then
    17.         Atom = GlobalFindAtom(MyAtomName)
    18.         If Atom = 0 Then
    19.             CurrAtom = GlobalAddAtom(MyAtomName)
    20.         Else
    21.             End
    22.         End If
    23.     Else
    24.         CurrAtom = GlobalAddAtom(MyAtomName)
    25.     End If
    26. End Sub
    27.  
    28. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    29.     GlobalDeleteAtom CurrAtom
    30. End Sub
    Last edited by CVMichael; Jan 4th, 2004 at 02:40 AM.

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