获取所有线程的名称

Dear SQL(燊) 2020-10-22 02:35:59
大神们,如何获取c#中所有线程的名称,如下代码获取名称“TestName”,非常感谢!


new Thread(() =>
{
Thread.CurrentThread.Name = "TestName";
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
Console.WriteLine("a"+Thread.CurrentThread.Name);
}
}).Start();
...全文
13376 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
我不懂电脑 2020-11-09
  • 打赏
  • 举报
回复
/*获取线程的名称*/
class MyThread3 implements Runnable{
public void run(){
for(int i=0; i<10; i++){
System.out.println(Thread.currentThread().getName()+"运行, "+i); //获取当前线程的名称
}
}
}
public class ThreadGetNameDemo {


public static void main(String[] args) {
// TODO Auto-generated method stub
MyThread3 mt = new MyThread3(); //定义Runnable子类对象
new Thread(mt,"第一个线程").start(); //手工设置线程的名称
new Thread(mt,"第二个线程").start();
}
}




/*
运行结果:
第一个线程运行, 0
第一个线程运行, 1
第一个线程运行, 2
第一个线程运行, 3
第一个线程运行, 4
第一个线程运行, 5
第一个线程运行, 6
第一个线程运行, 7
第一个线程运行, 8
第一个线程运行, 9
第二个线程运行, 0
第二个线程运行, 1
第二个线程运行, 2
第二个线程运行, 3
第二个线程运行, 4
第二个线程运行, 5
第二个线程运行, 6
第二个线程运行, 7
第二个线程运行, 8
第二个线程运行, 9
sand_kk 2020-11-07
  • 打赏
  • 举报
回复
引用 17 楼 Dear SQL(燊) 的回复:
@sand_kk,下以代码有时会报错:System.InvalidOperationException:“集合已修改;可能无法执行枚举操作。”
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();

   public static IEnumerable<Thread> ThreadList
        {
            get
            {
                lock (threadList)
                {
                    // return new ReadOnlyCollection<Thread>(threadList);
                    return threadList.ToList();
                }
            }
        }
这个地方这样改一下试试? 错误应该是由于遍历集合的途中开辟或结束了新线程,导致原有集合变化,遍历无效。 这样修改的话可以获得遍历时的线程,但是要注意下里面可能有在遍历中途就停止的线程, 或缺少在遍历过程中新加入的线程。
Dear SQL(燊) 2020-11-05
  • 打赏
  • 举报
回复
@sand_kk,下以代码有时会报错:System.InvalidOperationException:“集合已修改;可能无法执行枚举操作。”
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();
wanghui0380 2020-11-04
  • 打赏
  • 举报
回复
额,不知道你们到底讨论的是啥。 进程?线程? 当前进程?系统所有进程? 系统所有线程? 当前进程所有线程? 进程名?线程名? ps:线程有名?个人建议你们还是先把这些聊清楚在讨论 另外: 去微软官方下载procexp个看看,对照这个软件的运行界面,然后画框框给我们,看你到底要的是那块
八爻老骥 2020-11-04
  • 打赏
  • 举报
回复
Process.Threads
Dear SQL(燊) 2020-11-04
  • 打赏
  • 举报
回复
引用 10 楼 大西瓜大降价一块一斤 的回复:
这个只能自己存到List<Thread>里面,然后你自己再遍历出来
这样吗?也没有找到线程名称
var threads = System.Diagnostics.Process.GetCurrentProcess().Threads;
            var count = threads.Count;
            var actived = threads.Cast<ProcessThread>().ToList();
Dear SQL(燊) 2020-11-04
  • 打赏
  • 举报
回复
引用 11 楼 sand_kk 的回复:
查了不少资料,但都表示无法做到 https://stackoverflow.com/questions/1825882/getting-list-of-currently-active-managed-threads-in-net 中点赞最多的回答应该是最适合的解决方式了 (引用自上面链接的回答)

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}");
            }
        }
    }
正是我需要的,谢谢大侠!
sand_kk 2020-11-04
  • 打赏
  • 举报
回复
查了不少资料,但都表示无法做到 https://stackoverflow.com/questions/1825882/getting-list-of-currently-active-managed-threads-in-net 中点赞最多的回答应该是最适合的解决方式了 (引用自上面链接的回答)

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}");
            }
        }
    }
八爻老骥 2020-11-04
  • 打赏
  • 举报
回复


Process p = Process.GetCurrentProcess();
foreach (ProcessThread t in p.Threads)
{
Console.WriteLine(t.Id);
}

  • 打赏
  • 举报
回复
这个只能自己存到List<Thread>里面,然后你自己再遍历出来
Dear SQL(燊) 2020-11-03
  • 打赏
  • 举报
回复
自己顶 分不够可以加
Dear SQL(燊) 2020-10-23
  • 打赏
  • 举报
回复
引用 4 楼 wanghui0380 的回复:
var processes = Process.GetProcesses(); 我们不知道你要问啥,只能先给个这个,这是获取当前系统所有进程。想知道线程,当然是先找进程。拿到进程在遍历线程
我想遍历当前进程(这样进程是我开发的程序)下的线程,得到所有进程的名称,如下代码线程名称“TestName”
new Thread(() =>
            {
                Thread.CurrentThread.Name = "TestName";
                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine(i);
                    Console.WriteLine("a"+Thread.CurrentThread.Name);
                }
            }).Start();
Dear SQL(燊) 2020-10-22
  • 打赏
  • 举报
回复
引用 2 楼 strife013 的回复:
ThreadPool?
是的
wanghui0380 2020-10-22
  • 打赏
  • 举报
回复
var processes = Process.GetProcesses(); 我们不知道你要问啥,只能先给个这个,这是获取当前系统所有进程。想知道线程,当然是先找进程。拿到进程在遍历线程
Dear SQL(燊) 2020-10-22
  • 打赏
  • 举报
回复
引用 1 楼 The 祺℡ 的回复:
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();
        }
    }
}
这是进程名
strife013 2020-10-22
  • 打赏
  • 举报
回复
ThreadPool?
The 祺℡ 2020-10-22
  • 打赏
  • 举报
回复
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();
}
}
}

110,547

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧