[RESOLVED] Which Startup Object To Use?
Howdy,
First off, I'm a bit of a newbie so patience is appreciated... I have a couple question about setting the startup object for a project that I'm working on, which so far has the following two components: (in addition to a few other various classes)
- Main Form, which contains a single user interface window, a Timer subroutine which comprises the main section of code for the form, and a bunch of event handlers.
- Main module, which declares a number of public variables, has several functions and subs that are mostly private, and has a Sub Main that carries out most of the program startup, setup of some threading, etc.
I think I'd like the Sub Main to be the startup object, but the program is primarily event driven and when Sub Main finishes executing the program exits. Right now I have a msgbox at the end of Sub Main, which is a temporary fix. With the msgbox open, the Timer subroutine and threads continues to do what they're supposed to, and everything works fine except the focus switches from the Main Form to the msgbox (although the form is mostly for display anyway, and all the display items continue to function). When the msgbox is acknowledged, the program exits. Is there some other way to pause execution at the end of Sub Main while still allowing the everything else in the thread to work (like the msgbox does?)
When I use the Main Form as the startup object, the program doesn't automatically exit (which is good), but then all the declarations in Sub Main are missed, and I haven't figured out how to placed them in the Main Form class so that everything still works (I dunno... maybe some re-definition of namespace might solve that?)
Hopefully that sufficiently explains my dilemma. In short, I have no clear idea of which way to go with the startup object.
Re: Which Startup Object To Use?
What version of VB.NET are you using? If it's 2005 or later then you shouldn't really be using a Main method as your entry point anyway. You should use a form as the startup object and then use the application's Startup and Shutdown events to do extra processing.
If you're using 2003 or earlier then you can use a Main method but you can't call Show on your main form in it because Show is non-blocking, which means that execution will continue and reach the end of the Main method, which is where the application exits. You need to call Application.Run and pass your main form as an argument. Application.Run will not return until that form is closed.
Re: Which Startup Object To Use?
According to your profile you are using VB6. If this is correct for this question then your only choices are a Form object or a Module's Sub Main.
Awainting confirmation.
Re: Which Startup Object To Use?
Sorry - this is in VB.Net 2008. I originally started the project in VB6 and have recently converted it to VB.Net 2008.
Re: Which Startup Object To Use?
Sounds like the consensus is that I should use the form as the startup object, so I’ll try that approach then.
In that case, I have another question…
Currently I have a number of declarations at the start of the Main module, but these seem to be ignored if I use the form as the startup object. If I move these to the form class, then they exist in the name space of the form class, rather than the application (I’m not sure if that’s the right terminology – please correct me if not). Is there some easy way to declare these at startup outside of the form class in the overall application name space? … i.e. so I can avoid having to add “[name of instance of form class].” In front of variables/objects all the time.
Re: Which Startup Object To Use?
You'll want to handle the Application.Startup event to initialise global variables. You can access it by navigating to your project's properties page and clicking the button View Application Events, then selecting the startup event from the drop down list or just adding the following code manually within the class:
Code:
Private Sub MyApplication_Startup( _
ByVal sender As Object, _
ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs _
) Handles Me.Startup
End Sub
Re: Which Startup Object To Use?
Many thanks Max, but I may need another wee hint here. I don't think I can declare public variables in that class, so can I somehow initialize my existing module from MyApplication_Startup?
Re: Which Startup Object To Use?
Quote:
Originally Posted by Donkeyxote
Many thanks Max, but I may need another wee hint here. I don't think I can declare public variables in that class, so can I somehow initialize my existing module from MyApplication_Startup?
If you have a module in your project then it's public members are accessible anywhere in the project, which is the whole point. As such you certainly can initialise them from the Startup event handler. That said, why not just initialise them in the module itself?
Also, I actually like to declare my global variables (on the very rare cases that I need them) in the MyApplication class, which is where Startup event handler is located. You can declare them right there in the same file if you want but, given that that code file is named "ApplicationEvents" I prefer to keep it exclusively for application events. To declare global variables I simply create another partial class. Notice that, by default, the ApplicationEvents file contains only this code:
vb.net Code:
Namespace My
Partial Friend Class MyApplication
End Class
End Namespace
You can add a new code file to your project, perhaps named "ApplicationFields", and add the same code. You can then add field or property declarations for your globals, e.g.
vb.net Code:
Namespace My
Partial Friend Class MyApplication
Public MaxIterations As Integer = 100
End Class
End Namespace
Whether you use the ApplicationEvents file or create your own, you can now access that field in code via My.Application, e.g.
vb.net Code:
For i As Integer = 1 To My.Application.MaxIterations
I like the feel of accessing a global variable via My.Application, although it doesn't really offer any technical advantage over using a module.
Re: Which Startup Object To Use?
Update: I seems that just calling module.Main from MyApplication_Startup does the trick (I guess all the variables in the module must be declared before Main can run --- duh). So I'm in business! Thanks for all your help.