NameThreadForDebugging -- Naming threads for debugging
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.
What was wrong with my code?
The
NameThreadForDebugging
method is actually a static method. The second parameterThreadId
is optional and it equals the current thread id by default.
If I do not give aThreadId
clearly, I might very possibly name a the current thread, not the thread I really want to name it.
What is the solution?
Call
MyThread.NameThreadForDebugging('a_name', MyThread.ThreadId);
anywhere or CallNameThreadForDebugging('a_name');
at the beginning ofTMyThread.Execute
.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的更多相关文章
- Threading in C#
http://www.albahari.com/threading/ PART 1: GETTING STARTED Introduction and Concepts C# supports par ...
- How to Analyze Java Thread Dumps--reference
原文地址:http://architects.dzone.com/articles/how-analyze-java-thread-dumps The Performance Zone is pres ...
- 不完全翻译:Threading in C#-Getting Started
Introduction(引入,介绍) and Concepts(概念) 原文地址:http://www.albahari.com/threading/ 注:水平有限不能全文翻译,备注了个别字段和短句 ...
- How to Analyze Java Thread Dumps
When there is an obstacle, or when a Java based Web application is running much slower than expected ...
- Rock the Tech Interview
Today, Infusion held a talk in Columbia University about tech interview. Talker: Nishit Shah @ Infus ...
- 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 ...
- SOS.dll (SOS Debugging Extension)
SOS.dll (SOS Debugging Extension) lays threads associated with a live thread. The -special option di ...
- Debugging Chromium on Windows
转自:https://www.chromium.org/developers/how-tos/debugging-on-windows For Developers > How-Tos & ...
- Debugging JTAG Connectivity Problems
2013-12-04 22:34:26 转自:http://processors.wiki.ti.com/index.php/Debugging_JTAG_Connectivity_Problems ...
随机推荐
- uploadify scriptData参数无法传参的问题
最近需要使用到uploadify,需要向后台传递参数,使用script最多只能够传递一个参数,当然也可以通过合并参数然后再在服务器段拆分参数的方法来传递多个参数,而uploadify插件提供的scri ...
- Linux makefile教程之使用变量五[转]
使用变量 ———— 在 Makefile中的定义的变量,就像是C/C++语言中的宏一样,他代表了一个文本字串,在Makefile中执行的时候其会自动原模原样地展开在所使 用的地方.其与C/C++所不同 ...
- oracle收集
1. 高级sql学习——with子句 http://blog.chinaunix.net/uid-10697776-id-2935678.html 2.java List 排序 Collections ...
- 怎样学法学?——民法学泰斗王利明教授的演讲 z
今晚我讲“怎样学习法律”,但不是讲一般的学习法学的方法,而是主要从法学.法律的特征讲起.(因为)我们学习任何东西,都首先要搞清楚我们的学习对象有什么特征.性质. 我们要了解法律.法学本身的性质,要了解 ...
- 一些JS周边工具
Visual Studio JS 辅助插件 JScript Editor Extensions 功能: 1. 代码块折叠 2. 方法参数智能提示 3. 代码块Outlining ...
- nagios监控远程主机服务可能出现的问题
1.使用插件NRPE监控命令不存在 在添加服务的时候,命令配置文件中需要传递一个参数,那么在监控服务配置文件中,需要添加一个!表示后面的为参数. 出现未定义的命令,查看被监控主机上的配置文件,添加监控 ...
- Linux 权限相关
Linux中,所有文件都有 三种权限:User ,Group,Other 三个文件: /etc/passwd :包括所有系统账号,一般用户身份和root信息 /etc/shadow :保存个人密码 / ...
- font-size的探究
整理网上的资料 font字体,用px,em,100%,rem?分什么情况考虑? 我们逐渐意识到,我们用px作为文字大小的单位,已经出现很多问题.最主要是体现在用户不能灵活的控制文字的大小. 对于大多数 ...
- [转]Javascript定义类的三种方法
作者: 阮一峰 原文地址:http://www.ruanyifeng.com/blog/2012/07/three_ways_to_define_a_javascript_class.html 将近2 ...
- Chapter 7 Windows下pycaffe的使用之draw_net.py
Chapter 6 中完成了在Windows下,对pycaffe的编译,如果编译存在问题,请参考:http://www.cnblogs.com/xiaopanlyu/p/6158902.html 本文 ...