从ProcessThread到托管线程 [英] Getting from ProcessThread to a managed thread

查看:188
本文介绍了从ProcessThread到托管线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们无法复制的生产环境中,我们经常会挂起Windows服务的关闭.可能要过几个月才能再次发生.

Periodically we get a hang on shut down of a Windows service in a production environment that we just cannot reproduce. It can be months before it happens again.

我正在使用一些诊断程序来尝试解决该问题,我正在考虑的一件事是在我们启动应用程序关闭后60秒钟内将事件添加到系统线程池中.我们的应用程序应在10秒内彻底关闭.

I'm putting in some diagnostics to try and help with the issue, one thing I'm looking at is adding an event to the system thread pool for 60 seconds after we initiate shut down of the application. Our application should shutdown cleanly within 10 seconds maximium.

在这种情况下,我想将进程的其余运行线程追踪到事件日志中.

In this event I would like to trace out the remaining running threads of the process to the event log.

我可以使用System.Diagnostics.Process.GetCurrentProcess.Threads获取正在运行的线程.这些线程对象具有本地Win32线程ID等.

I can get the running threads using System.Diagnostics.Process.GetCurrentProcess.Threads. These threads objects have native Win32 thread id's etc.

我想知道是否有任何方法可以从这些线程ID返回到它们在当前进程中表示的任何托管线程. 我尝试这样做的原因是,对于我们产生的线程池和其他线程,我们给出代表其目的的名称,这确实有助于将它们取回.

I was wondering if there was any way of getting back from these thread id's to any managed threads that they represent in the current process. The reason I am trying to do this is because for our thread pools and other threads we spawn we give the names which represent their purpose and it would really help to get these back.

推荐答案

由于以下原因,这是不可能的. 从MSDN引用:

This is impossible for the following reason. Quote from MSDN:

操作系统ThreadId没有 与受托人的固定关系 线程,因为非托管主机可以 控制之间的关系 托管和非托管线程. 具体来说,复杂的主机可以 使用CLR Hosting API安排时间 针对相同的许多托管线程 操作系统线程,或移动 不同之间的托管线程 操作系统线程.

An operating-system ThreadId has no fixed relationship to a managed thread, because an unmanaged host can control the relationship between managed and unmanaged threads. Specifically, a sophisticated host can use the CLR Hosting API to schedule many managed threads against the same operating system thread, or to move a managed thread between different operating system threads.

因此托管线程和OS线程之间没有一对一的映射.

So there's no one-to-one mapping between managed threads and OS threads.

所以现在的问题是,如何获取当前在当前进程中运行的所有托管线程的列表?不幸的是我不知道答案.

So now the question becomes, how to get a list of all the managed threads currently running in the current process? Unfortunately I don't know the answer.

这篇关于从ProcessThread到托管线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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