http://forums.devart.com/viewtopic.php?t=16907

type
tagTHREADNAME_INFO = record
dwType : LongWord; // Must be 0x1000.
szName : PAnsiChar; // Pointer to name (in user addr space).
dwThreadId : LongWord; // Thread ID (-=caller thread).
dwFlags : LongWord; // Reserved for future use, must be zero.
end; procedure NameThreadForDebugging (threadId: dword; threadName: AnsiString);
const
MS_VC_EXCEPTION = $406D1388;
{$ifdef VER7P}
var tni : tagTHREADNAME_INFO;
{$endif}
begin
{$ifdef VER7P}
if (threadName <> '') and (DebugHook <> ) then begin
tni.dwType := $;
tni.szName := PAnsiChar(threadName);
tni.dwThreadID := threadID;
tni.dwFlags := ;
try
RaiseException(MS_VC_EXCEPTION, , SizeOf(tni) div SizeOf(LongWord), @tni);
except end;
end;
{$endif}
end;
NameThreadForDebugging($FFFFFFFF, 'DBMonitor send thread')

http://stackoverflow.com/questions/23343765/best-practice-for-naming-a-thread

Best practice for naming a thread

Recently I worked on a high concurrent event-driven framework (Java Akka), which will create massive Actor threads. When I debug an Akka application, threads have very meaningful names. It'd really awesome. When I switch back to Delphi, I feel upset that all threads are unnamed, although they are unnamed for the past 20 years already.

For all my own designed thread classes, I follow such a pattern that I define a setter SetThreadName and I call NameThreadForDebugging in the Execute method. This works fine so far.

type
TMyThread = class(TThread)
private
FThreadName: string;
protected
procedure Execute; override;
public
procedure SetThreadName(const ThreadName: string);
end; procedure TMyThread.SetThreadName(const ThreadName: string);
begin
FThreadName := ThreadName;
end; procedure TMyThread.Execute;
begin
NameThreadForDebugging(FThreadName);
// Put normal thread execution code here
end;

But those instances of 3rd-party threads will stay unnamed, unless I create a descent thread class.

Is there Delphi Magic to set SetThreadName to the base Thread class?

I can use Detour.pas to force NameThreadForDebugging(FThreadName) be called at the first place of Execute method.

Any ideas?

Update 1 Thanks David for his kindly help. To help other readers to understand my question well, the question has been sightly rephrased.

  1. What was wrong with my code?

    The NameThreadForDebugging method is actually a static method. The second parameter ThreadId is optional and it equals the current thread id by default.
    If I do not give a ThreadId clearly, I might very possibly name a the current thread, not the thread I really want to name it.

  1. What is the solution?

    Call MyThread.NameThreadForDebugging('a_name', MyThread.ThreadId);anywhere or Call NameThreadForDebugging('a_name'); at the beginning of TMyThread.Execute.

  2. Why so confused to make things right?

    I do not understand, why not provide a non-static version without the second ThreadId. If there was such a non-static version, I could not have made this mistake.

I'm extemporising here, but it looks to me as though you believe that it is only possible to name a thread from code executing inside that thread.

But that is not the case. In order to name a thread all you need is its ID.

The documentation gives the function signature as so:

class procedure NameThreadForDebugging(AThreadName: AnsiString;
AThreadID: TThreadID = TThreadID(-)); static;

If you don't supply the optional thread ID parameter then -1 is passed which is interpreted as meaning, the executing thread.

Which is how you have been using NameThreadForDebugging thus far. However, you can just pass the thread ID.

Since you clearly have thread instances, you also have their IDs at hand.

The interface that you have imagined involves calling an instance method of the thread passing the name of the thread.

That is you imagine writing this code:

Thread.SetThreadName(ThreadName);

Instead of doing that, you can simply write:

TThread.NameThreadForDebugging(ThreadName, Thread.ThreadID);

If you want to use a class helper you can do it like this:

type
TThreadHelper = class helper for TThread
public
procedure SetThreadName(const ThreadName: string);
end; procedure TThreadHelper.SetThreadName(const ThreadName: string);
begin
TThread.NameThreadForDebugging(ThreadName, ThreadID);
end;
procedure TMainForm.FormCreate( Sender : TObject );
begin
TThread.CurrentThread.NameThreadForDebugging( 'Main' );
TThread.NameThreadForDebugging( 'Main', MainThreadID );
TThread.NameThreadForDebugging( 'Main' );
end;

NameThreadForDebugging -- Naming threads for debugging的更多相关文章

  1. Threading in C#

    http://www.albahari.com/threading/ PART 1: GETTING STARTED Introduction and Concepts C# supports par ...

  2. How to Analyze Java Thread Dumps--reference

    原文地址:http://architects.dzone.com/articles/how-analyze-java-thread-dumps The Performance Zone is pres ...

  3. 不完全翻译:Threading in C#-Getting Started

    Introduction(引入,介绍) and Concepts(概念) 原文地址:http://www.albahari.com/threading/ 注:水平有限不能全文翻译,备注了个别字段和短句 ...

  4. How to Analyze Java Thread Dumps

    When there is an obstacle, or when a Java based Web application is running much slower than expected ...

  5. Rock the Tech Interview

    Today, Infusion held a talk in Columbia University about tech interview. Talker: Nishit Shah @ Infus ...

  6. Visual Studio 2010初学者的调试指南:Mastering Debugging in Visual Studio 2010 - A Beginner's Guide

    Introduction In the software development life cycle, testing and defect fixing take more time than a ...

  7. SOS.dll (SOS Debugging Extension)

    SOS.dll (SOS Debugging Extension) lays threads associated with a live thread. The -special option di ...

  8. Debugging Chromium on Windows

    转自:https://www.chromium.org/developers/how-tos/debugging-on-windows For Developers‎ > ‎How-Tos‎ & ...

  9. Debugging JTAG Connectivity Problems

    2013-12-04 22:34:26 转自:http://processors.wiki.ti.com/index.php/Debugging_JTAG_Connectivity_Problems ...

随机推荐

  1. 嵌入式 hi3518平台指定网卡测试是否通外网

    版权声明:本文为博主原创文章,未经博主允许不得转载. /********************************** (C) COPYRIGHT *********************** ...

  2. 嵌入式 Linux线程同步读写锁rwlock示例

    读写锁比mutex有更高的适用性,可以多个线程同时占用读模式的读写锁,但是只能一个线程占用写模式的读写锁.1. 当读写锁是写加锁状态时,在这个锁被解锁之前,所有试图对这个锁加锁的线程都会被阻塞:2. ...

  3. Files

    write public static void write(CharSequence from, File to, Charset charset) throws IOException { asC ...

  4. 解决Asp.net Mvc返回JsonResult中DateTime类型数据格式的问题

    问题背景: 在使用asp.net mvc 结合jquery esayui做一个系统,但是在使用使用this.json方法直接返回一个json对象,在列表中显示时发现datetime类型的数据在转为字符 ...

  5. windows系统下Python环境的搭建及Selenium的安装

    1.首先访问http://www.python.org/download/去下载最新的python版本: 2.下载安装包,一路安装完毕: 3.为计算机添加安装目录搭到环境变量,如图把python的安装 ...

  6. php生成百度新闻源xml

    <?php /* http://baike.baidu.com/view/125547.htm#2 百度网新闻开放协议 */ mysql_connect($CFG['db_host'] ,$CF ...

  7. EF selection expression 与 Linq备忘

    一.左连接查询 var query = people.GroupJoin(pets, person => person, pet => pet.Owner, (person, petCol ...

  8. Mapreduce执行过程分析(基于Hadoop2.4)——(一)

    1 概述 该瞅瞅MapReduce的内部运行原理了,以前只知道个皮毛,再不搞搞,不然怎么死的都不晓得.下文会以2.4版本中的WordCount这个经典例子作为分析的切入点,一步步来看里面到底是个什么情 ...

  9. leetcode oj s_06

    public class Solution { public static void main(String[] args) { String s = "PAYPALISHIRING&quo ...

  10. tinyxml2简单使用

    引入头文件 <span style="font-size:18px;">#include "HelloWorldScene.h" #include ...