在Delphi中命名的线程是什么? [英] Named threads in Delphi - what is that for?

查看:116
本文介绍了在Delphi中命名的线程是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您使用BDS中的工具调色板创建TThread后代时,可以为该线程提供一个名称。这是自动生成的代码。您只需在Execute方法中调用SetName()函数,调用此方法的线程将以一种奇怪的方式给出一个名称...

When you create a TThread descendant using the tool palette in your BDS, you can provide a name for the thread. Here's the auto-generated code. You just call the SetName() function in the Execute method and the thread calling this method is given a name in a kind of weird way...

{$IFDEF MSWINDOWS}
type
  TThreadNameInfo = record
    FType: LongWord;     // must be 0x1000
    FName: PChar;        // pointer to name (in user address space)
    FThreadID: LongWord; // thread ID (-1 indicates caller thread)
    FFlags: LongWord;    // reserved for future use, must be zero
  end;
{$ENDIF}

{ TTestThread }

procedure TTestThread.SetName;
{$IFDEF MSWINDOWS}
var
  ThreadNameInfo: TThreadNameInfo;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
  ThreadNameInfo.FType := $1000;
  ThreadNameInfo.FName := 'ThreadName';
  ThreadNameInfo.FThreadID := $FFFFFFFF;
  ThreadNameInfo.FFlags := 0;

  try
    RaiseException( $406D1388, 0, sizeof(ThreadNameInfo) div sizeof(LongWord), @ThreadNameInfo );
  except
  end;
{$ENDIF}
end;

我发现调试时非常有用,您不仅可以看到TID,还可以看到线程名称办法。你知道哪个线程是感谢的。

I find it really useful during debugging for you can see not only TIDs, but also thread names assigned that way. You know which thread is which thanks to that.

然而,请告诉我,如果可以以任何方式访问分配的名称。它可以根据线程的句柄来读取吗?或者是否可以从另一个进程的外部过程读取?你知道,有一些应用程序列出你的进程和在其中工作的线程。

Please tell me, however, if the name assigned can be accessed in any way. Can it be read based on a thread's handle? Or can it be read even from 'outside' the process by another process? You know, there are applications which list your processes and the threads working in them. Will this name be accessible to apps like that?

谢谢!

推荐答案

p>实际上,线程名称只是用于调试目的,没有别的,真的。在代码中,您可以使用ThreadID来识别线程。如果你想保留这些线程ID的名称,请保留一个单独的(字典)列表,将每个线程ID映射到任何你喜欢的名字。

你看到的黑客是一个讨厌的技巧。被调用的异常被调试器捕获,调试器只是把它作为一个特殊的异常处理,只是继续执行。异常标志只是告诉系统在引发异常后继续,因为代码将处理它。空的except-clause正在处理代码中的异常。与调试器进行通信只是一个肮脏的技巧,它会引发异常并记住你刚刚传给它的名字。

Actually, thread names are just used for debugging purposes and nothing else, really. In your code, you could just identify threads by using the ThreadID. And if you want to keep a name with those thread ID's, keep a separate (dictionary) list which maps each thread ID to whatever name you like.
The hack that you see does a nasty trick. The exception that is raised is captured by the debugger, which just handles it as a special exception and will just continue execution. The exception flag just tells the system to continue after the exception is raised, since the code will handle it. The empty except-clause is handling the exception within your code. It's just a dirty trick to communicate with the debugger, which will zee the exception and remember the name you've just passed to it...

这篇关于在Delphi中命名的线程是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆