PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VB.net (vs 2013) - Most efficient way to open and close forms-VBForums
Results 1 to 15 of 15

Thread: VB.net (vs 2013) - Most efficient way to open and close forms

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    8

    VB.net (vs 2013) - Most efficient way to open and close forms

    I have a VB.net program that if I test any one piece, it works 100% error free, and runs quickly. However, if I keep using it, eventually it starts to lock up as I try to go between forms. The only thing I can think of is that I am not opening and closing each form cleanly and that they are still running when I try to close them. What is the most efficient way to open and close a VB.net form?

    Since my program has had performance issues, I ripped all logic out of my forms, and this is my current logic (yep this is ALL of it):

    Login FORM button logic:
    form1.Show()
    Me.Hide()

    form1 button logic:
    form2.Show()
    Me.close()

    form2 button1 logic:
    form3.Show()
    Me.close()

    form2 button2 logic:
    form1.Show()
    Me.close()

    form3 button logic:
    form1.Show()
    Me.close()

    Is there some sort of Visual studio property setting I need to set that I am missing?
    Please advise. Thank you.
    Last edited by slaze1catalpa; Jan 30th, 2018 at 03:56 PM.

  2. #2
    Fanatic Member
    Join Date
    Sep 2017
    Posts
    668

    Re: VB.net (vs 2013) - Most efficient way to open and close forms

    Where frmMain is your form name:
    Code:
            Dim MyForm As New frmMain
            MyForm.ShowDialog()

  3. #3
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: VB.net (vs 2013) - Most efficient way to open and close forms

    There are any number of reasons why a form might leak resources every time it's opened and closed, the way you open and close it is rarely the reason. I'd have to see a lot more code to know.

    The biggest culprit is usually: "When the form is created and displayed, it creates some things that implement IDisposable, but you did not update its Dispose() method or write any other code to deal with that." If those are purely managed things, the garbage collector can keep up. But if they reference any unmanaged resources, the memory they created is lost and can't get reclaimed until you close the application.

    So I can't offer anything but very general advice. Normally you can open and close forms forever without having a problem.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  4. #4
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    3,825

    Re: VB.net (vs 2013) - Most efficient way to open and close forms

    If your closing the form then it's closed and not running. It doesn't matter if you use "Show" or "ShowDialog", closed is closed but with Showdialog you will have to close the form before you can move on to some other operation. Are you using the default instance or are you creating your own form instance (like kpmc showed). Do the forms work with Public objects? Really need more information and post some relevant code.

  5. #5
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: VB.net (vs 2013) - Most efficient way to open and close forms

    Actually, there's a difference between Show() and ShowDialog().

    When you Close() after Show(), the form is completely disposed. That means all of the designer controls are destroyed, any Dispose() override is called, etc. It's gone.

    ShowDialog() is meant to let the form hang around so you can get values out of its controls. So if you Close() a Form after ShowDialog() is called, most of it hangs around in memory. You're supposed to call Dispose() on it when you're done.

    Welcome to the world of "How APIs were designed in the early .NET era, when we didn't know what the heck we were doing."
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,383

    Re: VB.net (vs 2013) - Most efficient way to open and close forms

    Quote Originally Posted by kpmc View Post
    Where frmMain is your form name:
    Code:
            Dim MyForm As New frmMain
            MyForm.ShowDialog()
    That's not wrong in and of itself but it probably doesn't help here. Firstly, while I would tend to recommend against using default instances, there's nothing inherently wrong in doing so. As long as you don't need more than one instance open at the same time, you don't actually lose anything by using the default instance.

    One thing to note, though, is that your code creates an instance but never disposes it, so it will create multiple undisposed instances if it's executed more than once. If you use the default instance then you will at least be using the same instance repeatedly, so it will actually be more resource-friendly. That may also be an issue if you make changes to data in the form and don't want those changes to show in the form the next time it's displayed.

    The other issue is that you are recommending ShowDialog instead of Show. Those two methods do slightly different things and you should always use the one that behaves the way you need in any particular situation. It would be wrong to call ShowDialog if you didn't want modal dialogue behaviour.

    Regardless, I doubt that the changes you suggest, even if they did suit the scenario, would fix the problem mentioned. As Sitten Spynne says, the root cause is likely to be elsewhere and we'd need more information to diagnose it.
    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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,383

    Re: VB.net (vs 2013) - Most efficient way to open and close forms

    Quote Originally Posted by Sitten Spynne View Post
    When you Close() after Show(), the form is completely disposed. That means all of the designer controls are destroyed, any Dispose() override is called, etc. It's gone.
    Not quite. You can still access properties of a disposed form or control. What you can't do is anything that makes use of its Handle, because that is the unmanaged resource that gets released when the object is disposed. There might be other changes too, e.g. the Text of a form is empty after disposal, but you can still get the Text property. If you add your own properties to a form and don't clear them on disposal, you'll be able to get their values 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

  8. #8
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    3,825

    Re: VB.net (vs 2013) - Most efficient way to open and close forms

    jmc,

    I'm confused,
    One thing to note, though, is that your code creates an instance but never disposes it, so it will create multiple undisposed instances if it's executed more than once.
    Are you saying that triggering the form close event doesn't dispose of the form when you create an instance?

    MSDN says,
    When a form is closed, all resources created within the object are closed and the form is disposed.
    Or are you just talking about if you never trigger the close event.

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,383

    Re: VB.net (vs 2013) - Most efficient way to open and close forms

    Quote Originally Posted by wes4dbt View Post
    jmc,

    I'm confused,


    Are you saying that triggering the form close event doesn't dispose of the form when you create an instance?

    MSDN says,


    Or are you just talking about if you never trigger the close event.
    There is no Close event. There's a Close method and there are Closing and Closed events (that should no longer be used) and FormClosing and FormClosed events.

    I mean what Sitten Spynne posted earlier. If you display a form by calling Show then closing that form will dispose it. If you display the form by calling ShowDialog though, closing the form will NOT dispose it. That's why you should generally create a modal dialogue with a Using statement, e.g.
    vb.net Code:
    1. Using f As New SomeForm
    2.     If f.ShowDialog() = DialogResult.OK Then
    3.         '...
    4.     End If
    5. End Using
    If you want to prove this to yourself, try the following code:
    vb.net Code:
    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2.     Dim f2 As New Form2
    3.  
    4.     f2.Show()
    5.     f2.Close()
    6.  
    7.     MessageBox.Show(f2.IsDisposed.ToString())
    8. End Sub
    9.  
    10. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    11.     Dim f2 As New Form2
    12.  
    13.     f2.ShowDialog()
    14.  
    15.     MessageBox.Show(f2.IsDisposed.ToString())
    16. End Sub
    In the second case, you'll obviously have to close the dialogue yourself. You can do that by clicking the Close button on the title bar or you can add a Button and call Close in its Click event handler but, however you do it, you'll find that the form is not disposed.

    Note that you took this quote from the documentation:
    When a form is closed, all resources created within the object are closed and the form is disposed.
    but you missed the very next paragraph:
    The two conditions when a form is not disposed on Close is when (1) it is part of a multiple-document interface (MDI) application, and the form is not visible; and (2) you have displayed the form using ShowDialog. In these cases, you will need to call Dispose manually to mark all of the form's controls for garbage collection.
    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

  10. #10
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: VB.net (vs 2013) - Most efficient way to open and close forms

    Quote Originally Posted by jmcilhinney View Post
    Not quite. You can still access properties of a disposed form or control. What you can't do is anything that makes use of its Handle, because that is the unmanaged resource that gets released when the object is disposed. There might be other changes too, e.g. the Text of a form is empty after disposal, but you can still get the Text property. If you add your own properties to a form and don't clear them on disposal, you'll be able to get their values too.
    I hadn't thought of this, but the situations where I'd use it are real narrow. The contract of IDisposable is "instances that have been disposed should throw ObjectDisposedException after disposal", so technically those properties are breaking contract. Life's easier if we all follow it. (This thread kind of demonstrates, the full explanation's confusing, the short "don't do it" explanation is a lie but easy to comprehend and safe!)

    But IDisposable is a really bad pattern, so this isn't exactly a hill I'm going to fight on.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  11. #11
    Fanatic Member
    Join Date
    Sep 2017
    Posts
    668

    Re: VB.net (vs 2013) - Most efficient way to open and close forms

    I use ShowDialog because you cant ignore it, and you cant interact with any other form while it's open. I was unaware it was not disposing.

  12. #12
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,383

    Re: VB.net (vs 2013) - Most efficient way to open and close forms

    Quote Originally Posted by kpmc View Post
    I use ShowDialog because you cant ignore it, and you cant interact with any other form while it's open.
    You should use ShowDialog when you want modal dialogue behaviour, but that doesn't mean that it is inherently better than Show. They are different and you should use whichever is appropriate in the specific circumstances.
    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

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,622

    Re: VB.net (vs 2013) - Most efficient way to open and close forms

    That doesn't feel like the answer to the original question, though. Normally, it likely doesn't matter which you use. I think there is something else going on, and we don't have enough information to answer it correctly.
    My usual boring signature: Nothing

  14. #14

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    8

    Re: VB.net (vs 2013) - Most efficient way to open and close forms

    Okay since there was a request for my logic, I editted my post. I removed every piece of code I could possibly remove, except the navigation, yet still it will start locking up after navigating from form1 to form3 after 5 or 6 times. Thanks in advance.

  15. #15
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,383

    Re: VB.net (vs 2013) - Most efficient way to open and close forms

    For future reference, please don't edit a post unless that post is wrong. If you want to add something, add it in a new post. If nothing else, it saves us having to scroll back to the top of the page to see the code every time. Also, please copy the actual code from the IDE if at all possible. It looks like you have written that code by hand right here. Sometimes that can't be avoided but I've lost count of the times people have done that and wound up posting something different to what they're actually using. Also, please use formatting tags to make your code more readable. If code comes from different places, e.g. different forms, then use separate tags to separate it. The clearer you can make your post to read, the more chance there is that we will be able to pick up where the issue is. Finally, it's often hard to impossible to find the source of an error simply by reading code. The best thing you can do is provide instructions for us to reproduce the problem. That would include the simplest code possible that demonstrates the issue at hand and nothing else and what we need to do after running that code to see the claimed behaviour.
    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width