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 ...
随机推荐
- 【转】VMware 11安装Mac OS X 10.10
VM11安装Mac OS X 10.10 网上竟没有搜到相似的内容,所以拿出来大家分享 工具/原料 1.VMware Workstation 11 2.unlocker 203(for OS X 插件 ...
- Bootstrap初级用户谈谈网页在手机上的显示效果优化
本人之前已经使用Bootstrap有一段时间了,但是之前做出的网站都只是在电脑端使用,没有注意过手机端的显示效果.这两天自己使用Bootstrap做了一个简单的Web个人日志系统,想在手机端也使用,桌 ...
- AndroidManifest修改重打包全过程
AndroidManifest修改重打包全过程: 作者: 蔡建良 2013-06-26 准备工具:apktool.jar和signapk.jar 下载: http://download.csdn.ne ...
- 关于Servlet的PrintWriter 中文乱码问题
ps:servlet的PrintWriter和ServletOutputStream是不能同时使用的,同时使用会抛异常; PrintWriter是字符流.ServletOutputStream是字节流 ...
- makefile实例(3)-多个文件实例优化
我们先看一下make是如何工作的在默认的方式下,也就是我们只输入make命令.那么,1.make会在当前目录下找名字叫“Makefile”或“makefile”的文件.2.如果找到,它会找文件中的第一 ...
- MAC OSX 驱动操作
mac ox系统的驱动安装常规操作:下载到 *.kext 的驱动以后,都可以直接把它拖到 /System/Library/Extensions/ 下替换掉原来的文件.替换了以后,还需要修复权限才能够正 ...
- selenium-grid2 远程并发控制用例执行
今天闲来无事,随意看了一下selenium,突然注意到grid这个功能以前都是,在读有关selenium的文档时候知道有这么个grid远程控制的功能,但一直没有去试过.所以呢,今天就简单的做了这么个小 ...
- bzoj 3270 博物馆(高斯消元)
[题意] 两人起始在s,t点,一人pi概率选择留在i点或等概率移动,问两人在每个房间相遇的概率. [思路] 把两个合并为一个状态,(a,b)表示两人所处的状态,设f[i]为两人处于i状态的概率.则有转 ...
- 怎么去掉Xcode工程中的某种类型的警告
XCode警告 问题描述 在我们的项目中,通常使用了大量的第三方代码,这些代码可能很复杂,我们不敢改动他们,可是作者已经停止更新了,当sdk升级或者是编译器升级后,这些遗留的代码可能会出现许许多 ...
- 英语之idiom
1 quick and dirty = Done or constructed in a hasty, approximate, temporarily adequate manner, but no ...