Results 1 to 19 of 19

Thread: [RESOLVED] Prevent Main Form from Continuing

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,991

    Resolved [RESOLVED] Prevent Main Form from Continuing

    It has been a while since I've done any WinForm applications and I'm running into a slight issue.

    I have the following in the constructor of my frmMain:
    Code:
    Sub New()
        Using loginForm = New frmLogin()
            If (loginForm.ShowDialog() <> DialogResult.OK) Then
                Close()
                Return
            End If
    
            My.Application.CurrentUser = loginForm.User
            InitializeComponent()
        End Using
    End Sub
    The idea is the user must be logged in for the form to show.

    But the issue that I'm running into is that if the user clicks on the close button in the login form, it is throwing this exception:
    System.ObjectDisposedException: 'Cannot access a disposed object.
    Object name: 'frmMain'.'
    My guess is that when I try to call close, Visual Basic is attempting to dispose of the object behind the scenes, but because it hasn't completed the constructor it is trying to access an object that hasn't been "created" yet.

    The work around that I thought of is to simply set a boolean flag based on the result of my If/Then condition and then in the Form's Load event, close the form if the flag is false. But that sounds a little hackish to be honest.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,736

    Re: Prevent Main Form from Continuing

    To terminate immediately without going any further...

    Code:
    End

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

    Re: Prevent Main Form from Continuing

    Not trying to be funny but just remove the Close button. Force them to use the Login button or press a Cancel button.

    Ah, I reread your post. So the line "Close()" is throwing the error. Thought it was the previous line that was the problem.

    Think I had this problem once and what I did was move the code to the form load event.
    Last edited by wes4dbt; Jan 11th, 2021 at 03:33 PM.

  4. #4

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,991

    Re: Prevent Main Form from Continuing

    End did it. Don't know why I didn't think of it.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,288

    Re: Prevent Main Form from Continuing

    Quote Originally Posted by dday9 View Post
    End did it. Don't know why I didn't think of it.
    Because it's absolutely terrible, that's why. Everything here is very wrong. Displaying a dialogue in a constructor? Utterly criminal! Check out the CodeBank thread below to learn the proper way to login in WinForms:

    https://www.vbforums.com/showthread....WinForms-Login

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,736

    Re: Prevent Main Form from Continuing

    Quote Originally Posted by jmcilhinney View Post
    Because it's absolutely terrible, that's why. Everything here is very wrong. Displaying a dialogue in a constructor? Utterly criminal! Check out the CodeBank thread below to learn the proper way to login in WinForms:

    https://www.vbforums.com/showthread....WinForms-Login
    You obviously analysed the code and the program flow better than i did. If i'd read it more thoroughly, maybe i wouldn't have recommended End

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,288

    Re: Prevent Main Form from Continuing

    Quote Originally Posted by .paul. View Post
    You obviously analysed the code and the program flow better than i did. If i'd read it more thoroughly, maybe i wouldn't have recommended End
    I would suggest that End is always terrible. It just depends how terrible. End can appear to be a "good" way to prevent certain code being executed on exit but any code can always be structured better such that it's not required. At most, an If statement or two may need to be added.

  8. #8

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,991

    Re: Prevent Main Form from Continuing

    Quote Originally Posted by jmcilhinney View Post
    Because it's absolutely terrible, that's why. Everything here is very wrong. Displaying a dialogue in a constructor? Utterly criminal! Check out the CodeBank thread below to learn the proper way to login in WinForms:

    https://www.vbforums.com/showthread....WinForms-Login
    Even better, thank you.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,736

    Re: Prevent Main Form from Continuing

    Quote Originally Posted by jmcilhinney View Post
    I would suggest that End is always terrible. It just depends how terrible. End can appear to be a "good" way to prevent certain code being executed on exit but any code can always be structured better such that it's not required. At most, an If statement or two may need to be added.
    If it wasn't considered worthy, it just wouldn't be part of the language...

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,288

    Re: Prevent Main Form from Continuing

    Quote Originally Posted by .paul. View Post
    If it wasn't considered worthy, it just wouldn't be part of the language...
    That's not true. There are a number of elements of VB.NET that are really just holdovers from VB6 that have been retained to avoid too much change in upgraded VB6 code. They didn't introduce an equivalent to End in C# for a good reason. End is bad for obvious reasons. If there's a better alternative, it should be used.

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,736

    Re: Prevent Main Form from Continuing

    Quote Originally Posted by jmcilhinney View Post
    That's not true. There are a number of elements of VB.NET that are really just holdovers from VB6 that have been retained to avoid too much change in upgraded VB6 code. They didn't introduce an equivalent to End in C# for a good reason. End is bad for obvious reasons. If there's a better alternative, it should be used.
    It still exists in VB2019... It's been a while since VB6 upgrades were a serious issue

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

    Re: Prevent Main Form from Continuing

    Quote Originally Posted by .paul. View Post
    It still exists in VB2019... It's been a while since VB6 upgrades were a serious issue
    Yeah, but they're not going to remove a keyword from the language and break every app that uses it. They tend to avoid breaking changes unless there's genuine benefit so they won't remove it just because it's cr*p. If that were the case, GoTo would have been removed long ago. The simple fact is that, in just about any .NET application, there are better ways to quit than using End so those alternatives should be used.

  13. #13
    Addicted Member
    Join Date
    Jun 2019
    Posts
    232

    Re: [RESOLVED] Prevent Main Form from Continuing

    End statement:

    The End statement stops code execution abruptly, and does not invoke the Dispose or Finalize method, or any other Visual Basic code. Object references held by other programs are invalidated. If an End statement is encountered within a Try or Catch block, control does not pass to the corresponding Finally block.
    It is dangerous as app may have opened connections, files or some objects that require explicit closing.

    But checking what End does is that it actually is a call to Microsoft.VisualBasic.CompilerServices.ProjectData.EndApp() and the source shows following code:
    VB.NET Code:
    1. Public Shared Sub EndApp()
    2.     FileSystem.CloseAllFiles(System.Reflection.Assembly.GetCallingAssembly())
    3.     System.Environment.Exit(0) 'System.Environment.Exit will cause finalizers to be run at shutdown
    4. End Sub

    So it is not so dangerous in most cases.

    To be back on topic: I prefer to disable application framework in project properties and use Sub Main() as startup and perform initialization of the winforms app similar to C#. Application.Run() accepts Form and ApplicationContext as overloads. Using ApplicationContext() is possible to add some extensive logic before loading the main form, including showing some other forms like network licensing form (connecting to server, getting free slot, etc.) and/or login form.

  14. #14
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,288

    Re: [RESOLVED] Prevent Main Form from Continuing

    Quote Originally Posted by peterst View Post
    Using ApplicationContext() is possible to add some extensive logic before loading the main form
    True indeed, or even showing no form at all. I have a CodeBank thread that loads the application into the system tray without creating a form at all. You can then use the tray menu to display forms or whatever else is required.

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,736

    Re: [RESOLVED] Prevent Main Form from Continuing

    These are interesting topics. I've never really ventured into that aspect of the VB architecture, having never really needing to in my apps. I'm up to my neck in Javascript, JQuery, and Ajax at the moment. It's taking it's toll on me, but i'm beginning to win. It's not always easy to help much in VB these days, I always have prior engagements. With ddays question, i didn't stop to wonder if he was doing it all wrong...

  16. #16

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,991

    Re: [RESOLVED] Prevent Main Form from Continuing

    Quote Originally Posted by .paul. View Post
    These are interesting topics. I've never really ventured into that aspect of the VB architecture, having never really needing to in my apps. I'm up to my neck in Javascript, JQuery, and Ajax at the moment. It's taking it's toll on me, but i'm beginning to win. It's not always easy to help much in VB these days, I always have prior engagements. With ddays question, i didn't stop to wonder if he was doing it all wrong...
    Same here, only with typescript which is basically if JavaScript and C# had a baby. It really has been a while since I've done any VB development.

  17. #17
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,213

    Re: [RESOLVED] Prevent Main Form from Continuing

    END isn't a holdover from VB6.... it's part and parcel of the BASIC language... been there since the beginning. It's baked into the lexicon, every bit as much as is IF. Once something has made it's way into a language's lexicon, it's maintainers a loathe to remove it, no matter how despised it may be by the developers. It looks like they've tried to make it as safe as possible, but there's still the possiblity that there's cleanup that doesn't happen. I still liken it to taking the key out of the ignition while doing 60 on the highway.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  18. #18
    Addicted Member
    Join Date
    Jun 2019
    Posts
    232

    Re: [RESOLVED] Prevent Main Form from Continuing

    In C# (and also VB as part of .NET ecosystem) there is another extreme application termination with different intent but similar results. It depends on application and developer how the exit is handled.

  19. #19

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,991

    Re: [RESOLVED] Prevent Main Form from Continuing

    Quote Originally Posted by techgnome View Post
    I still liken it to taking the key out of the ignition while doing 60 on the highway.
    I've done that too

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