Welcome to Dream.In.Code
Become a VB Expert!

Join 149,903 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,210 people online right now. Registration is fast and FREE... Join Now!




Getting around Form_QueryUnload causing loop.

 
Reply to this topicStart new topic

Getting around Form_QueryUnload causing loop.

geewhiz
16 Jan, 2008 - 02:15 PM
Post #1

New D.I.C Head
*

Joined: 20 Aug, 2007
Posts: 31


My Contributions
Here is my problem. I need a form unload check for my program to prevent accidental closing of a program. I also want only one instance of the program running at a time.

This is how I'm doing such in my main to prevent multi-instances:

CODE

Private Sub Form_Load()

    On Error Resume Next
    If App.PrevInstance Then
        MsgBox ("Only one instance of this program can be running."), vbExclamation, "The requested " & "application is already open"
        Unload Form
        End
    End If
    
    OtherFunctions
    RequestToCloseFlag = 1

End Sub


And doing this to check and question the unload query.

CODE

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    ' *** universal unload check
    If RequestToCloseFlag = 1 Then
        Dim strQuestion As String
        Dim intAnswer As Integer
        Dim aryMode As Variant

        aryMode = Array("Program Close?", _
            "vbFormCode", "vbAppWindows", _
            "vbAppTaskManager", "vbFormMDIForm")

        strQuestion = "This program should NEVER be closed!" & vbCrLf & "Are you sure you want to close?"

        intAnswer = MsgBox(strQuestion, vbQuestion + vbYesNo, _
        aryMode(UnloadMode))
        If intAnswer = vbNo Then
            Cancel = -1
            Exit Sub
        End If
        LogStatusByte = 4
        AppendLogFile
    End If

End Sub


The problem is that I get stuck in a loop doing this. If I already have 1 instance running and I open another, the App.PrevInstance triggers, on 'OK' it tries to Form Unload. But this then triggers the Form_QueryUnload which tries to run its code to prevent closing. No problem. That is why I was trying to use the RequestToCloseFlag variable to skip past all of the "Are you sure you want to close" stuff on a newly opened instance.

But it appears that any code execution within the Form_QueryUnload angers the App.PrevInstance, and causes it to just loop the messagebox "Only one instance of this program can be running."

Is there any way to stop the opening of my program with the App.PrevInstance without involving the Unload Form that triggers the Form_QueryUnload?
User is offlineProfile CardPM
+Quote Post

Banned
RE: Getting Around Form_QueryUnload Causing Loop.
17 Jan, 2008 - 01:12 AM
Post #2

New D.I.C Head
*

Joined: 18 Oct, 2007
Posts: 12


My Contributions
Hi tongue.gif :
Your code looks fine for me, but...

First:
Try to not use 'End' becouse finish the application but dont release the memory used by the application when it ends.

Second:
If you want to finish the aplication fast without the Form_QueryUnload event launch edit your code, that's not a good advise becouse it uses 'End' icon_down.gif .

CODE

...
    If App.PrevInstance Then
        MsgBox ("Only one instance of this program can be running."), vbExclamation, "The requested " & "application is already open"
        'Unload Me '<-- comment this line
        End
    End If

Third:
the 'App.PrevInstance' its the simple VB way to know if theres another instance of the same application running, if you want to avoid a new instance of your application you may try the API way with a loader:

In my example are two projects:
1.- <TargetExe> with the code you posted, make an exe called TargetExe.exe in the same 'app.path'.

2.- <Loader> Finds a window called TargetExe, if a window called 'TargetExe' its running it wont launch it again.

Fourth:
The Loader example dont work with MDI forms, becouse MDI forms dont have a stable window name (example: word documents), but there are API functions like EnumWindows and EnumChildWindows to do that kind of work.

I hope this works, if you need more info just let me know
tongue.gif
Attached File  Your_sample_code.zip ( 3.66k ) Number of downloads: 35

User is offlineProfile CardPM
+Quote Post

LookNAO
RE: Getting Around Form_QueryUnload Causing Loop.
17 Jan, 2008 - 11:01 AM
Post #3

D.I.C Head
**

Joined: 28 Dec, 2007
Posts: 66



Thanked: 1 times
My Contributions
Put the FindWindow DLL call at the top,
put the rest in your form load()...

This will close your current app and if your other app is minimized it will pull it to the foreground...

CODE

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long


Form Load()
Dim AppAlreadyRunning as string

If App.PrevInstance = True Then
    AppAlreadyRunning = Me.Caption
    'Rename your current app to goodbye
    Me.Caption = "GoodBye"
    'Bring up the already running app
    ShowWindow FindWindow(vbNullString,  AppAlreadyRuning), 9
    'We're outta here....
     End
End If



User is offlineProfile CardPM
+Quote Post

LookNAO
RE: Getting Around Form_QueryUnload Causing Loop.
17 Jan, 2008 - 11:12 AM
Post #4

D.I.C Head
**

Joined: 28 Dec, 2007
Posts: 66



Thanked: 1 times
My Contributions
Sorry forgot the showwindow...


QUOTE(LookNAO @ 17 Jan, 2008 - 12:01 PM) *

Put the FindWindow DLL call at the top,
put the rest in your form load()...

This will close your current app and if your other app is minimized it will pull it to the foreground...

CODE

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long


Form Load()
Dim AppAlreadyRunning as string

If App.PrevInstance = True Then
    AppAlreadyRunning = Me.Caption
    'Rename your current app to goodbye
    Me.Caption = "GoodBye"
    'Bring up the already running app
    ShowWindow FindWindow(vbNullString,  AppAlreadyRuning), 9
    'We're outta here....
     End
End If




User is offlineProfile CardPM
+Quote Post

Banned
RE: Getting Around Form_QueryUnload Causing Loop.
17 Jan, 2008 - 11:50 AM
Post #5

New D.I.C Head
*

Joined: 18 Oct, 2007
Posts: 12


My Contributions
geewhiz:
So many ways to do it dont you think???
User is offlineProfile CardPM
+Quote Post

geewhiz
RE: Getting Around Form_QueryUnload Causing Loop.
17 Jan, 2008 - 12:17 PM
Post #6

New D.I.C Head
*

Joined: 20 Aug, 2007
Posts: 31


My Contributions
Woot.
Thanks for the suggestions guys. I actually had a buddy suggest the idea of using a secondary proggy to check for instances before opening, or not opening, the main prog. But I was kinda hoping for an easier way. Got to thinking I could make it all fancy looking with a loading bar and prompts, so I was starting to move that direction. And you guys just hooked up both ways of doing it. Thanks for the direction, tips, and code!!!

Now let me see how carried away with this I can get.... biggrin.gif
User is offlineProfile CardPM
+Quote Post

geewhiz
RE: Getting Around Form_QueryUnload Causing Loop.
17 Jan, 2008 - 02:58 PM
Post #7

New D.I.C Head
*

Joined: 20 Aug, 2007
Posts: 31


My Contributions
LookNAO, your code doesn't seem to close the form. It does stop the program but there is still a skeleton form of the program on screen. Any ideas?
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 01:20PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month