Microsoft KB Archive/818439

= PRB: When You Change the Name of a Thread in Visual J# .NET, the New Name Does Not Appear in the Debugger =

Article ID: 818439

Article Last Modified on 5/24/2007

-

APPLIES TO


 * Microsoft Visual J# .NET 2003 Standard Edition
 * Microsoft Visual J# .NET 2003 Standard Edition

-



SYMPTOMS
Although you can change the name of a thread by setting the Name property, the new name of the thread does not appear in the debugger. Throughout the debugging session, the debugger displays the name that is set for that thread the first time (either through the constructor or when the Name property is set for the first time).



CAUSE
This behavior occurs because the debugger displays the name of the underlying user runtime thread. The underlying user runtime thread that implements the Thread class has the name set only one time. During the construction of the thread, the name of the thread is set when one of the following occurs:
 * The constructor that has the name parameter is called.

-or-
 * The Name property is set the first time.

You cannot change the name of the underlying runtime thread by changing the Name property.



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
 In Visual Studio .NET, use Visual J# to create a Console Application project.  In Class1.jsl, replace the existing code with the following sample code: public class TestThreads {   public static void main(String[] args) {       ThreadSample ts1 = new ThreadSample(&quot;Newton&quot;); ThreadSample ts2 = new ThreadSample; ts1.setName(&quot;Einstein&quot;); ts2.setName(&quot;Galileo&quot;); ts1.start; ts2.start; } }

class ThreadSample extends Thread {   public ThreadSample {       super; }

public ThreadSample(String name) {       super(name); }

public void run {       int i = 0; String threadName = Thread.currentThread.getName;

while(i < 10) //Set break points here, and then run the application. {           try {                Thread.currentThread.setName(threadName + i); sleep(i); }            catch(Exception e) {} System.out.println(getName + &quot; : &quot; + i++); }   } }  Set the breakpoint at the line in the code that has a comment that indicates the position. Build the application. On the Debug menu, click Start to start debugging the application. On the Debug menu, point to Windows, and then click Threads.</li> In the Threads debug windows, notice that after you set the name of a thread, you cannot change the name later by setting the Name property.

The thread name of the first thread appears in the debugger as Newton, although the code resets the Name property to Einstein later. When the second thread appears, it has the name Galileo. However, when you change the names of the threads in the Run method, the new names do not appear in the debugger.</li></ol>

You can never change the name of the main thread of the application by setting the Name property because the name for the underlying user runtime thread is already set by using the name main.

Except that the name does not appear in the debugger, the Name property works as expected. If you want to retrieve the name of the thread that has been set through the Name property, retrieve the name through the Name property again.

<div class="references_section">