0

I am developing a .Net profiler.. I use ILRewriting for this..

I need to trace the managed thread creations and destroys.

Need to know the threading related function that will be called at the beginning of the thread and during the end of the thread , i can inject my code to it and record whenever the event happens.

Any ideas about the default function that will be called at the time of thread creation and ends..??

OR else is there any other way to capture the managed thread creation and destroying events??

I know that we can trace by setting the threading event mask.. but i need to capture particular managed threads not all the threads..

4

1 回答 1

2

正如 Hans 所指出的,CLR 使用ThreadCreatedThreadDestroyed回调通知分析器线程创建/销毁。注意:如果运行时在线程终止之前关闭,那么您将不会获得 ThreadDestroyed 回调......但我认为您没有获得 ThreadDestroyed 回调的更可能原因是 IIS(我假设“页面加载”您指的是asp .NET页面)决定保留线程以供将来的请求作为优化,如果它认为它有足够的其他线程,它可能会决定稍后终止它。

另外,关于您对该问题的第二条评论, ThreadID 和ManagedThreadID之间没有关系。我相信 ThreadID 是对内部数据结构的引用(将其视为不透明的值,不要试图解释它),并且 ManagedThreadID 似乎是一个简单的数字,当线程首先进入托管代码时顺序分配。如果您想确定哪个 ThreadID 对应于哪个托管线程,我可以想到 3 个选项:

  • 使用ThreadNameChanged回调检查线程名称(注意:如果线程名称是在线程启动之前设置的,那么这将在 ThreadCreated 回调之前引发)
  • 使用ThreadAssignedToOSThread回调检查操作系统线程 ID
  • 让分析代码调用分析器以提供上下文(使用 pinvoke 或通过调用为此目的检测的方法)
于 2015-02-10T10:27:35.293 回答