Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.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# 如何知道正在运行或已完成项目的某些线程?_C#_Asp.net_Thread Safety_Threadpool - Fatal编程技术网

C# 如何知道正在运行或已完成项目的某些线程?

C# 如何知道正在运行或已完成项目的某些线程?,c#,asp.net,thread-safety,threadpool,C#,Asp.net,Thread Safety,Threadpool,我需要为我的c项目运行线程 因为我有一个int调用函数的列表 private void getAllThreads() { string name= Thread.CurrentThread.Name; } 范例 protected void Page_Load(object sender, EventArgs e) { list<int> lst=new list<i

我需要为我的c项目运行线程

因为我有一个int调用函数的列表

private void getAllThreads()
        {
            string name= Thread.CurrentThread.Name;
        }
范例

    protected void Page_Load(object sender, EventArgs e)
          {
            list<int> lst=new list<int>();

            lst.addrange(/// get list of int from data base);

            foreach(int number in lst)
            {
            call(number );
            }


getAllThreads();
            }


        public void call(int x)
        {
          Thread newThread = new Thread(() => { calc(x); });
          newThread.Start();
           newThread.name=x.tostring();
        }

        public void calc(int x)
        {
        ..... do something
        }
但它总是空的

我试着用

Thread.CurrentThread.ManagedThreadId
但它的价值不同于

 Thread.id

尝试使用适合这种情况的库。微软的反应式框架是完美的

代码如下:

protected void Page_Load(object sender, EventArgs e)
{
    List<int> lst = new List<int>(new[] { 1, 3, 3 });
    /// get list of int from data base);

    var query =
        from number in lst.ToObservable()
        from result in Observable.Start(() => calc(number))
        select result;

    query
        .Subscribe(
            result =>
            {
                /* can handle each result here */
            },
            () =>
            {
                /* all done here */
            });
}
这将自动为您管理所有线程


只需使用System.Reactive.Linq获取System.Reactive并添加;到您的代码。

最简单的方法是移动到等待/任务。旧版eventstread用于fire-and-forget线程,您不希望线程有任何响应。如果希望在线程完成时收到警报,则应使用BackgroundWorker的async/await。