RAD Studio |  |

Converting an Unnamed Thread to a Named Thread
See Also
Collapse All

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

1. Add the Windows unit to the uses clause of the unit your thread is declared in:

Copy Code

    //---------------------------------------------------------------------------
    uses
    Classes {$IFDEF MSWINDOWS} , Windows {$ENDIF};
    //---------------------------------------------------------------------------

1. Add the SetName method to your thread class in the interface section:

Copy Code

    //---------------------------------------------------------------------------
    type
    TMyThread = class(TThread)
    private
    procedure SetName;
    protected
    procedure Execute; override;
    end;
    //---------------------------------------------------------------------------

Copy Code

    //---------------------------------------------------------------------------
    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)
    {
    }
    }
    //---------------------------------------------------------------------------

1. Add the TThreadNameInfo record and SetName method in the implementation section:

Copy Code

    //---------------------------------------------------------------------------
    {$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.

1. Add a call to the new SetName method at the beginning of your thread's Execute method:

Copy Code

    //---------------------------------------------------------------------------
    procedure TMyThread.Execute;
    begin
    SetName;
    { Place thread code here }
    end;
    //---------------------------------------------------------------------------

Copy Code

    //---------------------------------------------------------------------------
    void __fastcall TMyThread::Execute()
    {
    SetName();
    //---- Place existing Execute method code here ----
    }
    //---------------------------------------------------------------------------

See Also

Naming a Thread

Assigning Separate Names to Similar Threads

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!