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. Shell教程2-变量

    Shell支持自定义变量. 定义变量 定义变量时,变量名不加美元符号($),如: 复制纯文本新窗口   variableName="value" 注意,变量名和等号之间不能有空格, ...

  2. ETL工具的评价

    评价项目 评价结果 备注 支持平台 SUN Solaris.HP-UX.IBM AIX.AS/400.OS/390.Sco UNIX.Linux.Windows 支持数据源  DB2.Informix ...

  3. ANT 发布项目中 build.xml 文件的详细配置

    xml 代码 <?xml version="1.0" encoding="UTF-8"?> <!-- name:对应工程名字 default: ...

  4. 我的格斗梦——张龙海(R.J)谈游戏动画师职业(转)

    编者按:他是一个生在东北,祖藉却是韩国的年轻人.从小生性好动的他觉得上课 学习十分枯燥,所以高中没毕业便辍学在家.但他仍是一个喜欢动漫.游戏的年轻人,因为热爱所以他用父母给的钱开始了求学之路,在之后的 ...

  5. B-Tree、B+Tree和B*Tree

    B-Tree(这儿可不是减号,就是常规意义的BTree) 是一种多路搜索树: 1.定义任意非叶子结点最多只有M个儿子:且M>2: 2.根结点的儿子数为[2, M]: 3.除根结点以外的非叶子结点 ...

  6. Request、Request.Form、Request.QueryString 用法的区别

    Request.Form:获取以POST方式提交的数据. Request.QueryString:获取地址栏参数(以GET方式提交的数据). Request:包含以上两种方式(优先获取GET方式提交的 ...

  7. centos6.3 安装配置redis

    1.下载安装 1.1 下载包 注:在http://download.redis.io/releases查询需要下载的版本 wget http://download.redis.io/releases/ ...

  8. 常用SQL整理

    整理了日常用到的一些sqls 1.插入表 insert into table_B select * from table_A 2.清空表 truncate table test #清空表,结构还存在d ...

  9. js中ajax如何解决跨域请求

    js中ajax如何解决跨域请求,在讲这个问题之前先解释几个名词 1.跨域请求 所有的浏览器都是同源策略,这个策略能保证页面脚本资源和cookie安全 ,浏览器隔离了来自不同源的请求,防上跨域不安全的操 ...

  10. 项目常用jquery/easyui函数小结

    #项目常用jquery/easyui函数小结 ##背景 项目中经常需要使用到一些功能,封装.重构.整理后形成代码沉淀,在此进行分享 ##代码 ```javascript /** * @author g ...