Thread Name

  • Thread starter Thread starter Bob Day
  • Start date Start date
B

Bob Day

Using vs2003, vb.net

I start a thread, giving it a name before start. Code snippet:

'give each thread a unique name (for later identification)
Trunk_Thread.Name = "Trunk_0_Thread"

' allow only 1 thread per line
Trunk_Thread.ApartmentState = ApartmentState.STA

' start thread instance
Trunk_Thread.Start()

1) In that thread, I would expect
Dim x As String = Thread.CurrentThread.Name

To return the the name Trunk_0_Thread, but instead returns nothting.

2) If I give the thread a name in its sub new

thread.currentthread.name= "Trunk_0_Thread"

then later in the thread

Dim x As String = Thread.CurrentThread.Name

returns the proper name.

Why does it not work the in 1) above the first time?

Please advise.

Bob Day
 
Hi Bob,

Just an idea - In your Form_Load or Sub Main, set the name of the current
thread. and see whether it makes any difference.

I do this as a matter of course whenever I work with Threads.
Thread.CurrentThread.Name = "Main"

Regards,
Fergus
 
Hi Bob,

Which project do you use, Console or Windows application?
Where do you run your code in the #1 you mention?
This will help me to troubleshoot the problem.
I look forward to hearing from you.


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.
 
The thread is a class with no GUI itself in a windows application. It looks
for incoming telephone calls. This is a very large class, but where I
execute
Dim x As String = Thread.CurrentThread.Name
does not change the results. It can be in sub new, or in a method later on,
both return x = nothing.

Please advise.

Bob
 
Hi Bob,

In your original post you said that setting the thread name in sub new was
successful. In this last post you said that x = T.CT.name in sub new gives
nothing.As you can't set the name before sub new, are you saying that you set
it and then it goes?

Putting that confusion aside, and assuming that it's not gone immediately
after the assignment, have you tried debug overkill to determine as closely as
possible what operation or function is wiping the name out. There must be a
pair of points at which 'it was here a minute ago' and 'now it's gone'.

You may like to post a cut-down project which exhibits this problem. It's
hard to guess at one like this.

Regards,
Fergus
 
Hi Bob,

Here is my code, you may have a try.

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim o As New TestThread
o.ThreadRun()
End Sub

Public Class TestThread
Dim td As Thread
Public Sub New()
' if the three line is moved to the ThreadRun, it works too.
td = New Thread(AddressOf hello)
td.Name = "TestThread"
td.ApartmentState = ApartmentState.STA
End Sub
Public Shared Sub hello()
Debug.WriteLine(Thread.CurrentThread.Name)
End Sub
Public Sub ThreadRun()
td.Start()
End Sub
End Class

Or can you post your code for us to reproduce the problem.

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.
--------------------
 
After application run, here is the results:
If Not Me.gcThread_Trunk Is Thread.CurrentThread Then

' x = nothing, x1 = thread name

Dim x As String = Thread.CurrentThread.Name

Dim x1 As String = Me.gcThread_Trunk.Name

End If

How can X be nothing? It must be some thread, with some name, or if you
don't name a thread will it show nothing?

X 1 = 000_thread_Trunk_0_Based (ie thread name in 3 positions below):

After me.gcThread_Trunk.start, x = nothing, which makes sense.

Before Application Run, x = the new threads name, which makes sense.

After Application run, x = nothing again, which does not make sense.

I will runs some tests to see if I can comeup with any other info.

Thanks!

Bob Day


Fergus Cooney said:
Hi Bob,

If your results are different to those expected, something has to be
causing it. If you package up that project, we can
confirm whether it's in the project or in your system.

Can you set the name of this nameless thread after Application.Run?
It's a write-once property so should fall over if
you try.

Have you tried
If Not Me.gtThread_Trunk Is Thread.CurrentThread Then
MsgBox ("Hell's Bells!")
End If
after Application.Run?

Is your testbed still using that Intel object? Could this be doing
something like invoking a delegate in a different
 
Hi Bob,

The project's behavior seems like the code after Application.Run
Dim x As String = Thread.CurrentThread.Name seems to return to the first
thread, so the result is same as the first one.
Have you tried to run my code for a test, or modify my code to reproduce
the problem?
If you have concern on this object please post here.


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.
--------------------
From: "Bob Day" <[email protected]>
References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
 
Back
Top