Results 1 to 11 of 11

Thread: Check if all forms of an application has been closed/disposed

  1. #1

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Check if all forms of an application has been closed/disposed

    How do you check if all instances of forms (or simply all forms of an application) has already been closed?
    ====================
    ほんとにどもありがとう!

    Rie Ishida

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Check if all forms of an application has been closed/disposed

    Normally, the program would exit at this point.
    If you chose not to use the application framework, you'd have to check it yourself.
    It is generally considered a good practice to keep track of all your forms (and other objects).

    If all references to your forms are null then all of them have been disposed.

  3. #3

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Re: Check if all forms of an application has been closed/disposed

    You see, I have an MDIform, and there are instances when a child form "goes out" of the MDIform even if I declared that form to be a child form of the MDIform (I'm using the singleton method in opening instances of forms, so that I can limit each form's instance count to one). I want to know how to check if any other form other than the MDI form is still running so that before I close the MDIform, it would first check if there are any other forms running other than the MDIform, and will ask the user to close those forms first.

    Or if there is some other way to close all the forms right away (like End, I guess).
    ====================
    ほんとにどもありがとう!

    Rie Ishida

  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Check if all forms of an application has been closed/disposed

    It is not advisable, but yes, you can use End (or Environment.Exit).
    What do you mean by 'singleton' method? And how a child form 'goes out'?
    If you keep a reference to your form you can always control its behavior. I don't understand why couldn't you just check whether they are null?

  5. #5

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Re: Check if all forms of an application has been closed/disposed

    Quote Originally Posted by cicatrix View Post
    What do you mean by 'singleton' method? And how a child form 'goes out'?
    If you keep a reference to your form you can always control its behavior.
    Like this... I place the following property in the designer code of the form (eg: frmBorrowingForm):

    Code:
    Private Shared _instanceBorrowingForm As frmBorrowingForm
        Public Shared ReadOnly Property Instance() As frmBorrowingForm
            Get
                If _instanceBorrowingForm Is Nothing OrElse _instanceBorrowingForm.IsDisposed Then
                    'There is no current instance so create one.                
                    _instanceBorrowingForm = New frmBorrowingForm
                End If
    
                'Return a reference to the one and only instance.          
                Return _instanceBorrowingForm
            End Get
        End Property
    And call its instance using this code:

    Code:
    frmBorrowing.instance.show()
    frmBorrowing.instance.activate()
    Quote Originally Posted by cicatrix View Post
    If you keep a reference to your form you can always control its behavior. I don't understand why couldn't you just check whether they are null?
    How do I do this?
    ====================
    ほんとにどもありがとう!

    Rie Ishida

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Check if all forms of an application has been closed/disposed

    If you want to close the application then call Application.Exit.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Re: Check if all forms of an application has been closed/disposed

    Quote Originally Posted by jmcilhinney View Post
    If you want to close the application then call Application.Exit.
    I would gladly do that, but after the MDIform is closed, I would have to show the hidden frmLogin, that's why I can't just call Application.Exit.
    ====================
    ほんとにどもありがとう!

    Rie Ishida

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Check if all forms of an application has been closed/disposed

    Quote Originally Posted by riechan View Post
    I would gladly do that, but after the MDIform is closed, I would have to show the hidden frmLogin, that's why I can't just call Application.Exit.
    I'm not sure what you're saying. You suggest End as a possible solution, which is evil, yet Application.Exit is no good. That doesn't seem to make sense.

    Also, you really shouldn't have a hidden login form. Display the login form when the user is logging in and then close it. If the user logs out and you want someone else to login then create a new login form and then close it when you're done with it too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Check if all forms of an application has been closed/disposed

    Quote Originally Posted by riechan View Post
    Like this... I place the following property in the designer code of the form (eg: frmBorrowingForm):

    Code:
    Private Shared _instanceBorrowingForm As frmBorrowingForm
        Public Shared ReadOnly Property Instance() As frmBorrowingForm
            Get
                If _instanceBorrowingForm Is Nothing OrElse _instanceBorrowingForm.IsDisposed Then
                    'There is no current instance so create one.                
                    _instanceBorrowingForm = New frmBorrowingForm
                End If
    
                'Return a reference to the one and only instance.          
                Return _instanceBorrowingForm
            End Get
        End Property
    And call its instance using this code:

    Code:
    frmBorrowing.instance.show()
    frmBorrowing.instance.activate()
    I see, then I suggest to move further from that (even though I don't undestand why don't you do it the normal way) - incorporate instance counters in forms and probaly a method which will dispose of all instances if needed.

    Normally I load a form like this:

    Code:
    Dim frm As New Form1
    frm.Show
    This way I can control my form by using the frm reference. If I call frm.Dispose method the form will cease to exist.

    If I have many forms then I create a collection of forms and can easily acces to any of my forms.

    Your approach strips you of the opportunity to know exactly how many forms are opened.

    @jmcilhinney - are you aware of the fact that Application.Exit does not necessarily lead to the end of application (just a side thought).
    Environment.Exit(0) will always end the application.

  10. #10

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Re: Check if all forms of an application has been closed/disposed

    @jm: I tried using Application.Exit, and got this error: Collection was modified; enumeration operation may not execute.

    Quote Originally Posted by cicatrix
    I see, then I suggest to move further from that (even though I don't undestand why don't you do it the normal way) - incorporate instance counters in forms and probaly a method which will dispose of all instances if needed.

    Normally I load a form like this:

    Code:

    Dim frm As New Form1
    frm.Show

    This way I can control my form by using the frm reference. If I call frm.Dispose method the form will cease to exist.

    If I have many forms then I create a collection of forms and can easily acces to any of my forms.

    Your approach strips you of the opportunity to know exactly how many forms are opened.
    I did load a couple of forms that way. If I declare this line with the public variables,

    Code:
    Dim frm As New Form1
    it only opens one form, right? How do I count all the open forms of the application and how do I make the newly-opened form a child form of the MDIform (even after it was opened by a child form as well)?
    ====================
    ほんとにどもありがとう!

    Rie Ishida

  11. #11
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Check if all forms of an application has been closed/disposed

    vb.net Code:
    1. ' Key can be of any type, I used string
    2.  
    3. Public fc As New Collections.Generic.Dictionary(Of String, Form)
    4.  
    5. ' If you don't want to use keys, you can use List collection instead
    6. fc.Add("myNewForm", New Form1)
    7.  
    8. fc("myNewForm").Show()
    9.  
    10. Debug.Print("Here are the loaded forms:")
    11. For Each KVPair In fc
    12.     Debug.Print(String.Format("Key {0}, form name {1}", KVPair.Key, KVPair.Value.Name))
    13. Next
    14.  
    15. Debug.Print(String.Format("Total: {0} forms are loaded", fc.Count))

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