Is this a good way of loading your App?

Denaes

Senior Contributor
Joined
Jun 10, 2003
Messages
956
Code:
    Sub Main()
        'Splash Screen
        'Becuase "Splash" is the chosen mode
        'it will count down from 5 and dissapear on its own.
        '"info will cause the splash screen to appear without
        ' the countdown.
        Dim Splash As New frmSplash("Splash")
        Splash.ShowDialog()
        Application.DoEvents()
        Splash.Dispose()

        'Start up the Login screen
        Dim Login As New frmLogin
        Login.ShowDialog()
        Application.DoEvents()
        'Once the Login in finished, it checks for a valid
        'user level and user name. 
        If Login.UserLevel <> "" Then
            'if there is a user name, it calls the main form.
            Dim MainForm As New frmMain(Login.UserName, Login.UserLevel)
            Login.Dispose()
            'I had to call this as a dialog to keep the subMain from quitting
            MainForm.ShowDialog()
        Else
            'No valid user logged in. Probobly canceled out.
            Login.Dispose()
        End If
        'End of subMain, program is over
    End Sub

Anything glaringly wrong or that I could do better to load my app?
 
Rather than displaying the MainForm as a dialog with MainForm.ShowDialog you could just use
Visual Basic:
Application.Run(MainForm)
this will allow your main form to process the windows message pump and prevent the app exiting till MainForm closes.
 
PlausiblyDamp said:
Rather than displaying the MainForm as a dialog with MainForm.ShowDialog you could just use
Visual Basic:
Application.Run(MainForm)
this will allow your main form to process the windows message pump and prevent the app exiting till MainForm closes.

So that loads the MainForm as it's own thread and allows the other to keep going (instead of being held up at the .ShowDialog spot) and dispose of the Login and subMain.

It works great. I just like to understand how it effects the program :)
 
It affects the application in the way PlausiblyDamp already told you :). The method will start a message loop for your whole application. It won't be started on the new thread as you said, to see that use the Application.Run method with a form and put some code after the call, the code after the method call will not be executed until the form is done. Basically Application.Run will start a message loop like you would create by yourself in C++.
 
mutant said:
It affects the application in the way PlausiblyDamp already told you :).

I'll take your word on that :D I'm know it works, I'm just trying to figure out why it's better than .ShowDialog and what it does.

PlausiblyDamp said:
this will allow your main form to process the windows message pump and prevent the app exiting till MainForm closes.

I don't doubt what he says, I just don't understand it.

It seems to act (in practical view to me) just like .ShowDialog did. I know it didn't, but I'm trying to understand how it differs and how/why I can/should use it in the future.

mutant said:
The method will start a message loop for your whole application. It won't be started on the new thread as you said, to see that use the Application.Run method with a form and put some code after the call, the code after the method call will not be executed until the form is done. Basically Application.Run will start a message loop like you would create by yourself in C++.

I think you give me too much credit sometimes :D

Apparently I'm way in over my head in understanding what exactly it's doing and this is the place to ask for help, not a tutoring session.

I'll look up "message loop" and try to straighten out my thoughts of what a thread was (I thought they were divergent paths an application could take simultaniously)

Thank you for giving me somewere to start :)
 
Windows is a message based based OS. Every application receives a message from Windows when something happens that would conern a specific window window. For example when you press the little X button on the top right of the window, Windows sends a message to the application telling it that you want to shut down the application. Now if the application is nice enough it will shut down, or if its evil it will keep going against your will ignoring the message. Another example of a message is WM_PAINT message which is sent to the window when it needs to redraw itself, thats how you get the Paint even in your .NET programs, the window receives the paint message and raises the Paint event for you. .NET Framework is nice enough to handle the message loop for you but if you want you can still help it process the messages by overriding the WndProc method of the Form.
 
Hi guys... I know this is out of time but I'll still put mi fingher on this one...

You all tell true things but never answered the guy's question...

What's the true difference between starting the app with Form.ShowDialog() or Application.Run(Form)?

My answer is... Nothing!
The message loop will still do, the subsequecial instructions after each one will still only run after the form is closed...

Except... We can't think thak the .net development department of Microsoft have nothing to do so they just do redundant methods... eheheh :D

Application.Run have 3 overloads:
1- Application.Run()
2- Application.Run(Form)
3- Application.Run(ApplicationContext)

Explained:
1- Begins running the standard message loop on the current thread without a form.
2- The same as above but with a form.
3- Enables you to start the application based on an object that olds the information about the form that "keeps the app alive".

Resume:
If you're comparing Form.ShowDialog() with Application.Run(Form)... don't bother... they're the same...
The thing is that application.Run have 2 more overloads the do the difference.

Hoppe this helped!

Alex :p
 
The principal difference between those two (I think).
It's that .ShowDialog() won't process any line of code until the form is closed.
While .Run(form) will launch the form and will continue processing line of code after.
(correct me if I'm wrong)

So... depending of what you want and how you code it... both are good
 
Take the word from me... There's no difference!
Application.Run(New Form1) it's just kind of "the politically correct way of doing it" just because it's a "Form independant" way of start the application.

the real difference is in the other 2 overloads...

Alex :p
 
I hate to dredge up an old post, but I need to correct Alex here.

After a multiple hour strugle with a splash screen I can assure you, there IS a difference between from.ShowModal() and Application.Run(Form).

The difference has to do with the Form.OwnedForms collection. Specificly, if there is anything in the OwnedForms collection and you attemp to .ShowModal() the form, you will get an error.
showDialog tried to set an ineligible form as owner. Forms cannot own themselves or their owners.

If you specify an owner form on the form.ShowModal(new form) the show modal will work, but the form will not show up in the task bar.

Using Application.Run(Form) is the same as setting the form to the startup object in the project properties as far as I can tell. And it will work fine even if you have forms in the .OwnedForms collection.

-Rick
 
Back
Top