CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2001
    Posts
    7

    Using End statement and Unhook without gpf

    Hi, I am doing some subclassing. I know that I must unhook before I use the End statement. But If I unhook then on next line the End statment is used to end the program it gpfs! I assume it has not fully unhooked. If I have two buttons one that unhooks and one the has the End statment, and I press the unhook button then the end button program shutsdown ok, I assume that it has had time to fully unhook. But this is not a professional way to end a program! ie tell users to shut a program down press this one button then the other one! It does close ok if the X in the top right hand corner is used however. I assume the X in the top right hand corner must handle the shut down of the program differently.

    Thanks for your help! I assume that this question is really a beginers question.


  2. #2
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Using End statement and Unhook without gpf

    You should never use the END statement except in the Form_Load event of the application startup form. So says Microsoft. END will terminate all program execution with absolutely no cleanup work. FIles will be left open, DataBase connections left open, memory allocations will remain causing memory leaks and all sort of other ugly problems.
    Terminating a application in a system routine is alomst guaranteed to cause problems.
    To shutdown your program properly, use the Unload statement. Then in the unload event, do something like this to cause all open forms to close.

    option Explicit

    private Sub Command1_Click()
    Unload me
    End Sub

    private Sub Form_Load()
    Load Form2 ' load some forms as a example
    Load Form3
    End Sub

    private Sub Form_Unload(Cancel as Integer)
    Dim f as Form
    for Each f In Forms
    If f.Caption <> me.Caption then Unload f
    next f
    End Sub



    Ths form unload event causes cleanup processing to occur. You may need to do some of your cleanup work such as unallocating memory you explicitely allocated or updating databases with that last record etc.
    The routine above will cause the unload event of all open forms to execute so they can clean up their mess.

    John G

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