Sub main & application run

B

Bob Day

Using VS 2003, VB.Net, MSDE...

The example below from help shows that the Sub Main contains an explicit
Application.Run. Is this required? Are there pros and cons of doing it
this way? I have not been using application.run on my Main_Thread (i.e. sub
main) and it seems to be Ok.

Under what condistions should you explicityly put an application.run in a
sub main (ie Main_Thread)?

Please advise.

Shared Sub Main()
' Starts the application.
Application.Run(New Form1())
End Sub

Thanks!

Bob Day
 
H

Herfried K. Wagner [MVP]

Bob,

* "Bob Day said:
Using VS 2003, VB.Net, MSDE...

The example below from help shows that the Sub Main contains an explicit
Application.Run. Is this required? Are there pros and cons of doing it
this way? I have not been using application.run on my Main_Thread (i.e. sub
main) and it seems to be Ok.

What would you use instead? Yes, 'Application.Run' is the preferred
method to start a message loop.
 
T

Tom Shelton

Using VS 2003, VB.Net, MSDE...

The example below from help shows that the Sub Main contains an explicit
Application.Run. Is this required? Are there pros and cons of doing it
this way? I have not been using application.run on my Main_Thread (i.e. sub
main) and it seems to be Ok.

Under what condistions should you explicityly put an application.run in a
sub main (ie Main_Thread)?

Please advise.

Shared Sub Main()
' Starts the application.
Application.Run(New Form1())
End Sub

Thanks!

If your starting a windows forms app from a sub main, then yes it is
required. Otherwise, your main form will appear and then close when the
main method exits. The application.Run method creates the message loop
for your main form, and also sets that form as the application context
- so when it closes the rest of the app closes. If you are using a form
as your startup object, VB just generates this code behind your back....

It is also possible to use the ShowDialog method of your main form from
the Sub Main - but that isn't really the recommended method.
 
P

Peter Huang

Hi Bob,

Thanks for posting in the community.

Did you have any concern on this issue?
If so please post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top