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



The information in this article applies to:

  • Microsoft Visual J# .NET (2002)
  • Microsoft Visual J# .NET (2003)

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.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. In Visual Studio .NET, use Visual J# to create a Console Application project.
  2. 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("Newton");
    		ThreadSample ts2 = new ThreadSample();
    		ts1.setName("Einstein");
    		ts2.setName("Galileo");
    		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() + " : " + i++);
    		}
    	}
    }
  3. Set the breakpoint at the line in the code that has a comment that indicates the position.
  4. Build the application.
  5. On the Debug menu, click Start to start debugging the application.
  6. On the Debug menu, point to Windows, and then click Threads.
  7. 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.
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.

REFERENCES

For additional information about the Thread class and the Name property, visit the following Microsoft Web site:Content Maintenance:2404

Modification Type:MajorLast Reviewed:8/7/2003
Keywords:kbAPI kbThread kbDebug kbprb KB818439 kbAudDeveloper