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.
//--------------------------------------------------------------------------- uses Classes {$IFDEF MSWINDOWS} , Windows {$ENDIF}; //---------------------------------------------------------------------------
//--------------------------------------------------------------------------- type TMyThread = class(TThread) private procedure SetName; protected procedure Execute; override; end; //---------------------------------------------------------------------------
//--------------------------------------------------------------------------- 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) { } } //---------------------------------------------------------------------------
//--------------------------------------------------------------------------- {$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; //---------------------------------------------------------------------------
//--------------------------------------------------------------------------- procedure TMyThread.Execute; begin SetName; { Place thread code here } end; //---------------------------------------------------------------------------
//--------------------------------------------------------------------------- void __fastcall TMyThread::Execute() { SetName(); //---- Place existing Execute method code here ---- } //---------------------------------------------------------------------------
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|