Results 1 to 9 of 9

Thread: [RESOLVED] Which Startup Object To Use?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2008
    Location
    Vancouver, BC, Canada
    Posts
    24

    Resolved [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.

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

    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.
    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

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Dec 2008
    Location
    Vancouver, BC, Canada
    Posts
    24

    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.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2008
    Location
    Vancouver, BC, Canada
    Posts
    24

    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.

  6. #6
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    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

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Dec 2008
    Location
    Vancouver, BC, Canada
    Posts
    24

    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?

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

    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:
    1. Namespace My
    2.  
    3.     Partial Friend Class MyApplication
    4.  
    5.     End Class
    6.  
    7. 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:
    1. Namespace My
    2.  
    3.     Partial Friend Class MyApplication
    4.  
    5.         Public MaxIterations As Integer = 100
    6.  
    7.     End Class
    8.  
    9. 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:
    1. 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.
    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

    Thread Starter
    Junior Member
    Join Date
    Dec 2008
    Location
    Vancouver, BC, Canada
    Posts
    24

    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.

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