C# 正在获取.NET中当前活动的托管线程列表?
对于“支持的日志信息”类型的函数,我想枚举并转储活动线程信息 我很清楚,比赛条件可能会使这些信息变得半不准确,但我想尝试获得最好的结果,即使它不是100%准确 我查看了Process.Threads,但它返回ProcessThread对象,我希望有一个Thread对象的集合,这样我就可以记录它们的名称,以及它们是否是后台线程 是否有这样一个集合可用,即使我调用它时它只是活动线程的快照 即C# 正在获取.NET中当前活动的托管线程列表?,c#,.net,multithreading,collections,C#,.net,Multithreading,Collections,对于“支持的日志信息”类型的函数,我想枚举并转储活动线程信息 我很清楚,比赛条件可能会使这些信息变得半不准确,但我想尝试获得最好的结果,即使它不是100%准确 我查看了Process.Threads,但它返回ProcessThread对象,我希望有一个Thread对象的集合,这样我就可以记录它们的名称,以及它们是否是后台线程 是否有这样一个集合可用,即使我调用它时它只是活动线程的快照 即 请注意,要清楚,我不是在询问Process.Threads,这个集合给了我很多,但不是我想要的全部。我想知道
请注意,要清楚,我不是在询问Process.Threads,这个集合给了我很多,但不是我想要的全部。我想知道我们的应用程序中当前使用了多少时间特定的命名线程(这意味着我以后必须考虑连接这两种类型的对象,但是名称比开始时的CPU时间更重要。)在应用程序中创建每个线程时,在查找中存储线程信息是否可行
当每个线程启动时,可以使用
AppDomain.GetCurrentThreadId()
获取其ID。稍后,您可以使用它来交叉引用从进程返回的数据。线程
如果您愿意用另一个包装类替换应用程序的线程
创建,则所述包装类可以为您跟踪活动和非活动的线程。下面是这种包装的最小可行外壳:
namespace ThreadTracker
{
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
public class TrackedThread
{
private static readonly IList<Thread> threadList = new List<Thread>();
private readonly Thread thread;
private readonly ParameterizedThreadStart start1;
private readonly ThreadStart start2;
public TrackedThread(ParameterizedThreadStart start)
{
this.start1 = start;
this.thread = new Thread(this.StartThreadParameterized);
lock (threadList)
{
threadList.Add(this.thread);
}
}
public TrackedThread(ThreadStart start)
{
this.start2 = start;
this.thread = new Thread(this.StartThread);
lock (threadList)
{
threadList.Add(this.thread);
}
}
public TrackedThread(ParameterizedThreadStart start, int maxStackSize)
{
this.start1 = start;
this.thread = new Thread(this.StartThreadParameterized, maxStackSize);
lock (threadList)
{
threadList.Add(this.thread);
}
}
public TrackedThread(ThreadStart start, int maxStackSize)
{
this.start2 = start;
this.thread = new Thread(this.StartThread, maxStackSize);
lock (threadList)
{
threadList.Add(this.thread);
}
}
public static int Count
{
get
{
lock (threadList)
{
return threadList.Count;
}
}
}
public static IEnumerable<Thread> ThreadList
{
get
{
lock (threadList)
{
return new ReadOnlyCollection<Thread>(threadList);
}
}
}
// either: (a) expose the thread object itself via a property or,
// (b) expose the other Thread public methods you need to replicate.
// This example uses (a).
public Thread Thread
{
get
{
return this.thread;
}
}
private void StartThreadParameterized(object obj)
{
try
{
this.start1(obj);
}
finally
{
lock (threadList)
{
threadList.Remove(this.thread);
}
}
}
private void StartThread()
{
try
{
this.start2();
}
finally
{
lock (threadList)
{
threadList.Remove(this.thread);
}
}
}
}
}
不确定您是否可以走这样的路线,但如果您能够在开发的早期阶段合并,它将实现目标。好吧,这就是我必须要做的,但我宁愿不这样做。System.Diagnostics.ProcessThreads
与System.Threading.Threads
直接相关吗?前者是一个OS线程,后者是一个托管线程。您需要thread类中哪些附加信息,而ProcessThread类中没有这些信息?操作系统线程ID与托管线程没有固定关系,因为非托管主机可以控制托管线程和非托管线程之间的关系。具体来说,复杂的主机可以使用CLR宿主API针对同一操作系统线程调度多个托管线程,或者在不同的操作系统线程之间移动托管线程。我需要线程的名称,以便识别正在运行的线程。当我通过支持渠道得到一个文件,该文件说,由于某种原因,在当前运行的25个线程中,有3个线程占用了过多的cpu时间,知道它们的线程Id对我没有好处,但知道它是“后台文件处理线程管理器”和“后台文件处理工人#1”,作为初学者。谢谢,看起来这是我必须使用的路线。
namespace ThreadTracker
{
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
public class TrackedThread
{
private static readonly IList<Thread> threadList = new List<Thread>();
private readonly Thread thread;
private readonly ParameterizedThreadStart start1;
private readonly ThreadStart start2;
public TrackedThread(ParameterizedThreadStart start)
{
this.start1 = start;
this.thread = new Thread(this.StartThreadParameterized);
lock (threadList)
{
threadList.Add(this.thread);
}
}
public TrackedThread(ThreadStart start)
{
this.start2 = start;
this.thread = new Thread(this.StartThread);
lock (threadList)
{
threadList.Add(this.thread);
}
}
public TrackedThread(ParameterizedThreadStart start, int maxStackSize)
{
this.start1 = start;
this.thread = new Thread(this.StartThreadParameterized, maxStackSize);
lock (threadList)
{
threadList.Add(this.thread);
}
}
public TrackedThread(ThreadStart start, int maxStackSize)
{
this.start2 = start;
this.thread = new Thread(this.StartThread, maxStackSize);
lock (threadList)
{
threadList.Add(this.thread);
}
}
public static int Count
{
get
{
lock (threadList)
{
return threadList.Count;
}
}
}
public static IEnumerable<Thread> ThreadList
{
get
{
lock (threadList)
{
return new ReadOnlyCollection<Thread>(threadList);
}
}
}
// either: (a) expose the thread object itself via a property or,
// (b) expose the other Thread public methods you need to replicate.
// This example uses (a).
public Thread Thread
{
get
{
return this.thread;
}
}
private void StartThreadParameterized(object obj)
{
try
{
this.start1(obj);
}
finally
{
lock (threadList)
{
threadList.Remove(this.thread);
}
}
}
private void StartThread()
{
try
{
this.start2();
}
finally
{
lock (threadList)
{
threadList.Remove(this.thread);
}
}
}
}
}
namespace ThreadTracker
{
using System;
using System.Threading;
internal static class Program
{
private static void Main()
{
var thread1 = new TrackedThread(DoNothingForFiveSeconds);
var thread2 = new TrackedThread(DoNothingForTenSeconds);
var thread3 = new TrackedThread(DoNothingForSomeTime);
thread1.Thread.Start();
thread2.Thread.Start();
thread3.Thread.Start(15);
while (TrackedThread.Count > 0)
{
Console.WriteLine(TrackedThread.Count);
}
Console.ReadLine();
}
private static void DoNothingForFiveSeconds()
{
Thread.Sleep(5000);
}
private static void DoNothingForTenSeconds()
{
Thread.Sleep(10000);
}
private static void DoNothingForSomeTime(object seconds)
{
Thread.Sleep(1000 * (int)seconds);
}
}
}