Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何计算.NET应用程序中并发线程的数量?_C#_Multithreading_Visual Studio_Debugging_Task Parallel Library - Fatal编程技术网

C# 如何计算.NET应用程序中并发线程的数量?

C# 如何计算.NET应用程序中并发线程的数量?,c#,multithreading,visual-studio,debugging,task-parallel-library,C#,Multithreading,Visual Studio,Debugging,Task Parallel Library,阅读之后,我仍然怀疑这是否是计算并发线程数的正确方法 我所看到的是,该方法统计Parallel.ForEach 它是表示同时运行线程数正确的并发线程数的同义词吗? 我不是专家,但我可以想象: 当线程的活动交换到某个地方以便以后继续时,线程可以重复使用 理论上,参与循环活动的线程在循环完成后会保留在线程池中,但不会重新用于另一个循环活动 或者扭曲实验(线程计数)纯度的可能性是什么 无论如何,如何直接计算.NET进程的运行线程数量,最好是在(C#)代码中 更新: 因此,如果要遵循并用于计数 di

阅读之后,我仍然怀疑这是否是计算并发线程数的正确方法

我所看到的是,该方法统计
Parallel.ForEach

它是表示同时运行线程数正确的并发线程数的同义词吗?
我不是专家,但我可以想象:

  • 当线程的活动交换到某个地方以便以后继续时,线程可以重复使用
  • 理论上,参与循环活动的线程在循环完成后会保留在线程池中,但不会重新用于另一个循环活动
  • 或者扭曲实验(线程计数)纯度的可能性是什么
无论如何,如何直接计算.NET进程的运行线程数量,最好是在(C#)代码中

更新:

因此,如果要遵循并用于计数

directThreadsCount = Process.GetCurrentProcess().Threads.Count;
然后输出是,在Release(ThreadScont==7)和Debug(ThreadScont==15)模式下都非常相似:

[Job 0 complete. 2 threads remaining but directThreadsCount == 7
[Job 1 complete. 1 threads remaining but directThreadsCount == 7
[Job 2 complete. 2 threads remaining but directThreadsCount == 7
[Job 4 complete. 2 threads remaining but directThreadsCount == 7
[Job 5 complete. 2 threads remaining but directThreadsCount == 7
[Job 3 complete. 2 threads remaining but directThreadsCount == 7
[Job 6 complete. 2 threads remaining but directThreadsCount == 7
[Job 9 complete. 2 threads remaining but directThreadsCount == 7
[Job 7 complete. 1 threads remaining but directThreadsCount == 7
[Job 8 complete. 0 threads remaining but directThreadsCount == 7
FINISHED
也就是说,当
System.Diagnostics.ProcessThread
给出
“此时类名无效”时,线程的数量不会减少,说明这是不正确的

我的结论正确吗?为什么不能使用
ProcessThread

C#控制台应用程序使用的代码:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Edit4Posting
{
public class Node
{

  public Node Previous { get; private set; }
  public Node(Node previous)
  {
    Previous = previous;
    }
  }
  public class Edit4Posting
  {

    public static void Main(string[] args)
    {
      int concurrentThreads = 0;
      int directThreadsCount = 0;
      int diagThreadCount = 0;

      var jobs = Enumerable.Range(0, 10);
      Parallel.ForEach(jobs, delegate(int jobNr)
      {
        int threadsRemaining = Interlocked.Increment(ref concurrentThreads);

        int heavyness = jobNr % 9;

        //Give the processor and the garbage collector something to do...
        List<Node> nodes = new List<Node>();
        Node current = null;
        //for (int y = 0; y < 1024 * 1024 * heavyness; y++)
        for (int y = 0; y < 1024 * 24 * heavyness; y++)
        {
          current = new Node(current);
          nodes.Add(current);
        }
        //*******************************
        //uncommenting next line gives: "Class name is not valid at this point"
        //diagThreadCount=System.Diagnostics.ProcessThread
        directThreadsCount = Process.GetCurrentProcess().Threads.Count;
        //*******************************
        threadsRemaining = Interlocked.Decrement(ref concurrentThreads);
        Console.WriteLine(
           "[Job {0} complete. {1} threads remaining but directThreadsCount == {2}",
            jobNr, threadsRemaining, directThreadsCount);
      });
      Console.WriteLine("FINISHED");
      Console.ReadLine();
    }
  }
}
使用系统;
使用System.Collections.Generic;
使用系统诊断;
使用System.Linq;
使用系统线程;
使用System.Threading.Tasks;
命名空间Edit4Posting
{
公共类节点
{
公共节点前一个{get;private set;}
公共节点(上一个节点)
{
先前=先前;
}
}
公共类编辑4职位
{
公共静态void Main(字符串[]args)
{
int concurrenthreads=0;
int directthreadscont=0;
int diagThreadCount=0;
变量作业=可枚举范围(0,10);
Parallel.ForEach(作业、委托(int-jobNr)
{
int threadsRemaining=联锁增量(参考concurrentThreads);
整数权重=作业编号%9;
//给处理器和垃圾收集器一些事情做。。。
列表节点=新列表();
节点电流=零;
//对于(整数y=0;y<1024*1024*重;y++)
对于(整数y=0;y<1024*24*重;y++)
{
当前=新节点(当前);
节点。添加(当前);
}
//*******************************
//取消注释下一行给出:“类名此时无效”
//diagThreadCount=System.Diagnostics.ProcessThread
directThreadsCount=Process.GetCurrentProcess().Threads.Count;
//*******************************
螺纹保持=联锁。减量(参考concurrentThreads);
控制台写入线(
“[作业{0}完成。{1}个线程剩余,但DirectThreadScont=={2}”,
jobNr、螺纹维护、directThreadsCount);
});
控制台。写入线(“完成”);
Console.ReadLine();
}
}
}
我不是专家,但我可以想象,当线程的活动在某处交换时,线程可以被重用

否-除了一些非常特殊的情况(您几乎肯定不需要担心)之外,线程不是可重入使用的。在当前迭代完成后,线程将被重用以运行另一个迭代(或其他任务),但在代码运行时不会被要求执行其他操作

所以你目前的方法基本上是合理的

无论如何,如何直接计算.NET中运行的线程数量

您可以查看perfmon,它将为您绘制图表。(如果进程在您的控制下,最简单的方法是启动进程,但让它等待输入,然后打开perfmon,从“进程”选项中添加计数器,选择“线程计数”作为要添加的计数器,并从下拉列表中将其限制为您感兴趣的进程。单击“确定”,然后使您的进程开始工作。)

编辑:要回答更新,请执行以下操作:

也就是说,线程的数量并没有减少,说明上面引用的方法是不正确的

不,它所显示的只是积极运行代码的线程数量减少了,但线程保持不变。这对于线程池来说是完全自然的

为什么不能使用
ProcessThread

您只是不恰当地使用了它。Jeppe说系统正在计算
ProcessThread
的实例,而您的代码试图将一个类分配给一个变量:

diagThreadCount=System.Diagnostics.ProcessThread

这只是无效代码。错误消息显示编译器意识到它是类型的名称,但您不能只为变量指定类型名称。

我认为有不同类型的线程。您正在运行的应用程序的操作系统线程可以用以下数目计算:

int number = Process.GetCurrentProcess().Threads.Count;

它似乎要计算
System.Diagnostics.ProcessThread
实例。也许您需要计算另一种线程,比如“托管线程”,所以我不确定我的答案是否符合您的要求。

理论上,
Thread
s和
ProcessThread
s可能不同。实际上,它们几乎总是一样的。@s点击一个非常简单的控制台应用程序,写出上述属性,给出
4
,有时是
3
。我认为它是一个单线程应用程序n、 当调试同一个应用程序(附加了调试器)时,数字要高得多,如
12
13
。在另一个应用程序中,如果我使用
System.Threading.Thread
类启动许多新线程,则
System.Diagnostics.ProcessThread
的计数将增加大约正确的数字。(在Windows 7 64位上尝试过。)
diagThreadCount=System.Diagnostics.ProcessThread
不是完整的代码,我无法完成它