Converting an Unnamed Thread to a Named Thread

From RAD Studio
Jump to: navigation, search

Go Up to Naming a Thread


You can convert an unnamed thread to a named thread. For example, if you have a thread class that was created using Delphi 6 or earlier, convert it into a named thread.

To convert an unnamed thread to a named thread in Delphi

  1. Add the Windows unit to the uses clause of the unit your thread is declared in:
    uses
        Classes {$IFDEF MSWINDOWS} , Windows {$ENDIF};
    
  2. Add the SetName method to your thread class in the interface section:
    type
    TMyThread = class(TThread)
    private
      procedure SetName;
    protected
      procedure Execute; override;
    end;
    
  3. Add the TThreadNameInfo record and SetName method in the implementation section:
     {$IFDEF MSWINDOWS}
     type
     TThreadNameInfo = record
     FType: LongWord;     // must be 0x1000
     FName: PChar;        // pointer to name (in user address space)
     FThreadID: LongWord; // thread ID (-1 indicates caller thread)
     FFlags: LongWord;    // reserved for future use, must be zero
       end;
     {$ENDIF}
     { TMyThread }
     procedure TMyThread.SetName;
     {$IFDEF MSWINDOWS}
     var
     ThreadNameInfo: TThreadNameInfo;
     {$ENDIF}
     begin
     {$IFDEF MSWINDOWS}
     ThreadNameInfo.FType := $1000;
     ThreadNameInfo.FName := 'MyThreadName';
     ThreadNameInfo.FThreadID := $FFFFFFFF;
     ThreadNameInfo.FFlags := 0;
     try
     RaiseException( $406D1388, 0, sizeof(ThreadNameInfo) div sizeof(LongWord), @ThreadNameInfo );
      except
       end;
     {$ENDIF}
     end;
    

    Note: Set TThreadNameInfo to the name of your thread class.

    The debugger sees the exception and looks up the thread name in the structure you pass in. When debugging, the debugger displays the name of the thread in the Thread Status box's thread ID field.
  4. Add a call to the new SetName method at the beginning of your thread's Execute method:
    procedure TMyThread.Execute;
    begin
      SetName;
      { Place thread code here }
    end;
    

To convert an unnamed thread to a named thread in C++Builder


You can convert an unnamed thread to a named thread. For example, if you have a thread class that was created using C++Builder 5 or earlier, convert it into a named thread using the following steps.

  1. Add the SetName method to your thread class:
    void TMyThread::SetName() {
    	THREADNAME_INFO info;
    	info.dwType = 0x1000;
    	info.szName = "MyThreadName";
    	info.dwThreadID = -1;
    	info.dwFlags = 0;
    	__try {
    		RaiseException(0x406D1388, 0, sizeof(info) / sizeof(DWORD),
    			(DWORD*)&info);
    	}
    	__except (EXCEPTION_CONTINUE_EXECUTION) {
    	}
    }
    

    Note: Set info.szName to the name of your thread class.

    The debugger sees the exception and looks up the thread name in the structure you pass in. When debugging, the debugger displays the name of the thread in the Thread Status box's thread ID field.

  2. Add a call to the new SetName method at the beginning of your thread's Execute method:
    void __fastcall TMyThread::Execute() {
    	SetName();
    	// ---- Place existing Execute method code here ----
    }
    

See Also