Thread.Start in sub main

B

Bob Day

Using VS 2003, VB.net , MSDE ....

I am confused by the help example below. In a sub main (whether in a module
or winform), do you have to explicitly start the Main_Thread, as they do in
this example? If the answer is not always, when and where would you and for
what reasons? I don't explicitly start the thread (although I give it a
name) in a module Sub Main, and it still lists the thread as being created
in the threads window.

Thanks!
Bob

Setting a Thread Name (Managed)

Public Class Form1 : Inherits Form
Class Needle
' This method will be called when the thread is started
Sub Baz()
Console.WriteLine("Needle Baz is running on another thread")
End Sub
End Class

Public Shared Sub Main()
Console.WriteLine("Thread Simple Sample")
Dim oNeedle As New Needle()
' Create a Thread object
Dim oThread As New Thread(AddressOf oNeedle.Baz)
' Set the Thread name to "MainThread"
oThread.Name = "MainThread"
' Starting the thread invokes the ThreadStart delegate
oThread.Start()
End Sub
End Class
 
A

Armin Zingler

Bob Day said:
Using VS 2003, VB.net , MSDE ....

I am confused by the help example below. In a sub main (whether in a
module or winform), do you have to explicitly start the Main_Thread,
as they do in this example?

No. It's an example how to start a thread and set it's name.
If the answer is not always, when and
where would you and for what reasons? I don't explicitly start the
thread (although I give it a name) in a module Sub Main, and it
still lists the thread as being created in the threads window.

If you don't need another thread, you don't have to create one.
 
P

Peter Huang

Hi Bob,

Thanks for posting in the community.

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you are confusing about the
example in MSDN help.(Setting a Thread Name (Managed))

I agree with Armin's suggestion that when the application start, the
program will run from the Sub Main(). in the third line in the Sub Main(),
the program will create a new thread, in the Line oThread.Start(), the
new created thread will start to run, it will run the Thread Procedure
Baz(), so the "MainThread" is the name of the new created thread, you can
specify its name to other string. The example is to demostrate how to set
the name of a new thread.

Usually, if you do not need to do multiple thread working, you do not need
to create the new thread. When the application starts to run, it will run
from a specified method(entry point, Startup Object), in this case, the
method is the Public Shared Sub Main() method.

A process can create one or more threads to execute a portion of the
program code associated with the process. The thread to run the Public
Shared Sub Main() will be created automatically, you do not need to create
it.

StartupObject Property
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/
vxlrfvslangprojprojectpropertiesstartupobject.asp



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

Peter Huang

Hi Bob,

Thanks for posting in the community.

Did my reply answer your question?

If you have any concern on this issue,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