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. xampp无法打开phpmyadmin解决方案

    如果设置了apache的端口号(如8890),那么不可以用自带的admin按钮打开,而是要加上端口(如localhost:8890/phpmyadmin/)

  2. PHP Framework安装

    Framework 1> 初始化 前提:服务器上已经装有 Apache/Nginx 和 MySQL 进入 hush-framework/hush-app/bin 目录(Linux 下需执行 ch ...

  3. Web安全测试学习笔记(Cookie&Session)

    一,Session:含义:有始有终的一系列动作\消息1, 隐含了“面向连接” 和“保持状态”两种含义2, 一种用来在客户端与服务器之间保持状态的解决方案3, 也指这种解决方案的存储结构“把××保存在s ...

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

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

  5. hdu 1074(状态压缩dp+记录路径)

    题意:给了n个家庭作业,然后给了每个家庭作业的完成期限和花费的实践,如果完成时间超过了期限,那么就要扣除分数,然后让你找出一个最优方案使扣除的分数最少,当存在多种方案时,输出字典序最小的那种,因为题意 ...

  6. Redis pipeline and list

    Redis Redis 是一个开源的基于内存的数据结构存储器.通常可作为数据库,缓存和消息中介.它支持的数据结构有:字符串.哈希表.列表.集合.支持范围查询的有序集合.位图.hyperloglogs和 ...

  7. php获取网站根目录

    php获取网站根目录方法一:<?phpdefine("WWWROOT",str_ireplace(str_replace("/","\\&quo ...

  8. Windows下配置使用WinPcap

     0.前提 windows: win7 x64 WinPcap版本:4.1.3 WinPcap开发包:4.1.2 目标:在VS2010中配置使用winpcap 获取目标计算机中安装的网卡列表  1.下 ...

  9. ifstream 流 判断文件是否结尾的函数eof(.xml

    pre{ line-height:1; color:#800080; font-size:16px;}.sysFunc{color:#627cf6;font-style:italic;font-wei ...

  10. leetcode:Roman to Integer(罗马数字转化为罗马数字)

    Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...