-
Jan 11th, 2021, 02:43 PM
#1
[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.
-
Jan 11th, 2021, 02:59 PM
#2
Re: Prevent Main Form from Continuing
To terminate immediately without going any further...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 11th, 2021, 03:27 PM
#3
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.
-
Jan 11th, 2021, 03:45 PM
#4
Re: Prevent Main Form from Continuing
End did it. Don't know why I didn't think of it.
-
Jan 11th, 2021, 11:11 PM
#5
Re: Prevent Main Form from Continuing
 Originally Posted by dday9
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
-
Jan 11th, 2021, 11:35 PM
#6
Re: Prevent Main Form from Continuing
 Originally Posted by jmcilhinney
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 12th, 2021, 12:11 AM
#7
Re: Prevent Main Form from Continuing
 Originally Posted by .paul.
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.
-
Jan 12th, 2021, 12:18 AM
#8
Re: Prevent Main Form from Continuing
 Originally Posted by jmcilhinney
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.
-
Jan 12th, 2021, 01:53 AM
#9
Re: Prevent Main Form from Continuing
 Originally Posted by jmcilhinney
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 12th, 2021, 04:11 AM
#10
Re: Prevent Main Form from Continuing
 Originally Posted by .paul.
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.
-
Jan 12th, 2021, 05:25 AM
#11
Re: Prevent Main Form from Continuing
 Originally Posted by jmcilhinney
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 12th, 2021, 05:50 AM
#12
Re: Prevent Main Form from Continuing
 Originally Posted by .paul.
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.
-
Jan 12th, 2021, 06:06 AM
#13
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:
Public Shared Sub EndApp()
FileSystem.CloseAllFiles(System.Reflection.Assembly.GetCallingAssembly())
System.Environment.Exit(0) 'System.Environment.Exit will cause finalizers to be run at shutdown
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.
-
Jan 12th, 2021, 06:29 AM
#14
Re: [RESOLVED] Prevent Main Form from Continuing
 Originally Posted by peterst
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.
-
Jan 12th, 2021, 06:41 AM
#15
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 12th, 2021, 09:10 AM
#16
Re: [RESOLVED] Prevent Main Form from Continuing
 Originally Posted by .paul.
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.
-
Jan 12th, 2021, 09:29 AM
#17
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
-
Jan 12th, 2021, 09:37 AM
#18
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.
-
Jan 12th, 2021, 09:50 AM
#19
Re: [RESOLVED] Prevent Main Form from Continuing
 Originally Posted by techgnome
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|