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 ...
随机推荐
- unity, setting standard shader by script
http://forum.unity3d.com/threads/change-standard-shader-render-mode-in-runtime.318815/
- 详解MySQL三项实用开发知识
其实项目应用的瓶颈还是在db端,在只有少量数据及极少并发的情况下,并不需要多少的技巧就可以得到我们想要的结果,但是当数据量达到一定量级的时 候,程序的每一个细节,数据库的设计都会影响到系统的性能.这里 ...
- C# 类的多态、结构、接口、抽象、虚函数总结
多态: 类的多态是通过在子类(派生类)中重载基类的虚方法或成员函数来实现的. 可见,重载和虚函数的重写,并在调用时用父类装箱子类对象,是实现多态的一种重要的编程方式. 接口: 接口是一种用来定义程序的 ...
- n个数的最小公倍数
Description 求n个数的最小公倍数. Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数. Output 为每组测试数据输出它们的最小公倍数,每个测 ...
- mac 配置Python集成开发环境(Eclipse +Python+Pydev)
1.下载Mac版64位的Eclipse. 进入到Eclipse官方网站的下载页面(http://www.eclipse.org/downloads/),我选择了下图所示的软件包, 浏览器在下载过程中使 ...
- 使用Python拼接多张图片
写机器学习相关博文,经常会碰到很多公式,而Latex正式编辑公式的利器.目前国内常用的博客系统,好像只有博客园支持,所以当初选择落户博客园.我现在基本都是用Latex写博文,然后要发表到博客园上与大家 ...
- 我来说说MVC过滤器
APS.NET MVC中的每一个请求,都会分配给相应的控制器和对应的行为方法去处理,而在这些处理的前前后后如果想再加一些额外的逻辑处理.这时候就用到了过滤器. 在Asp.netMvc中当你有以下及类似 ...
- Name-based virtual servers 给予名称的虚拟服务
nginx first decides which server should process the request. Let’s start with a simple configuration ...
- JavaScript操作DOM的那些坑
js在操作DOM中存在着许多跨浏览器方面的坑,本文花了我将近一周的时间整理,我将根据实例整理那些大大小小的“坑”. DOM的工作模式是:先加载文档的静态内容.再以动态方式对它们进行刷新,动态刷新不影响 ...
- Scrum之Sprint物件
产品订单(Product Backlog) 一个需求的列表. 一般情况使用用户故事来表示backlog条目 理想情况每个需求项都对产品的客户或用户有价值 Backlog条目按照商业价值排列优先级 优先 ...