110,547
社区成员
发帖
与我相关
我的任务
分享
new Thread(() =>
{
Thread.CurrentThread.Name = "TestName";
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
Console.WriteLine("a"+Thread.CurrentThread.Name);
}
}).Start();
public static IEnumerable<Thread> ThreadList
{
get
{
lock (threadList)
{
// return new ReadOnlyCollection<Thread>(threadList);
return threadList.ToList();
}
}
}
这个地方这样改一下试试?
错误应该是由于遍历集合的途中开辟或结束了新线程,导致原有集合变化,遍历无效。
这样修改的话可以获得遍历时的线程,但是要注意下里面可能有在遍历中途就停止的线程, 或缺少在遍历过程中新加入的线程。new TrackedThread(() =>
{
Thread.CurrentThread.Name = "TestName";
//Thread.Sleep(100);
}).Thread.Start();
new TrackedThread(() =>
{
Thread.CurrentThread.Name = "TestName2";
Thread.Sleep(10000);
}).Thread.Start();
foreach (var thread in TrackedThread.ThreadList)
{
Console.WriteLine($"{thread.Name}");
}
Console.ReadKey();
var threads = System.Diagnostics.Process.GetCurrentProcess().Threads;
var count = threads.Count;
var actived = threads.Cast<ProcessThread>().ToList();
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);
}
}
}
}
}
使用方式
using ThreadTracker;
class Program
{
static void Main(string[] args)
{
new TrackedThread(() =>
{
Thread.CurrentThread.Name = "TestName";
Thread.Sleep(1000);
}).Thread.Start();
new TrackedThread(() =>
{
Thread.CurrentThread.Name = "TestName2";
Thread.Sleep(1000);
}).Thread.Start();
foreach (var thread in TrackedThread.ThreadList)
{
Console.WriteLine($"{thread.Name}");
}
}
}
Process p = Process.GetCurrentProcess();
foreach (ProcessThread t in p.Threads)
{
Console.WriteLine(t.Id);
}
new Thread(() =>
{
Thread.CurrentThread.Name = "TestName";
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
Console.WriteLine("a"+Thread.CurrentThread.Name);
}
}).Start();
using System;
using System.Diagnostics;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
foreach(Process p in Process.GetProcesses())
{
Console.WriteLine(p.ProcessName);
}
Console.Read();
}
}
}