Conversion d'un thread anonyme en thread nommé
De RAD Studio (Français)
Remonter à Exécution d'objets thread - Index
Vous pouvez convertir un thread anonyme en thread nommé. Si, par exemple, vous avez une classe thread créée avec Delphi 6 ou une version antérieure, convertissez-la en un thread nommé.
Pour convertir un thread anonyme en thread nommé
- Ajoutez l'unité Windows à la clause uses de l'unité dans laquelle votre thread est déclaré :
//---------------------------------------------------------------------------
uses
Classes {$IFDEF MSWINDOWS} , Windows {$ENDIF};
//---------------------------------------------------------------------------
-
Ajoutez la méthode SetName à votre classe thread dans la section interface :
//--------------------------------------------------------------------------- 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) { } } //---------------------------------------------------------------------------
-
Ajoutez l'enregistrement TThreadNameInfo et la méthode SetName dans la section implementation :
//--------------------------------------------------------------------------- {$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; //---------------------------------------------------------------------------Remarque : Définissez TThreadNameInfo sur le nom de votre classe thread.
- Le débogeur trouve l'exception et recherche le nom du thread dans la structure transmise. Lors du débogage, le débogeur affiche le nom du thread dans le champ ID thread de la boîte Etat des threads.
-
Ajoutez un appel à la méthode SetName au début de la méthode Execute de votre thread :
//--------------------------------------------------------------------------- procedure TMyThread.Execute; begin SetName; { Place thread code here } end; //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- void __fastcall TMyThread::Execute() { SetName(); //---- Place existing Execute method code here ---- } //---------------------------------------------------------------------------