C# 带有等待的ThreadAbortException

C# 带有等待的ThreadAbortException,c#,multithreading,exception,task,async-await,C#,Multithreading,Exception,Task,Async Await,我正面临一只奇怪的虫子。 我有大约100个长时间运行的任务,我想同时运行其中的10个 我在这里发现了与我的需求非常相似的东西:在节流部分 这里是简化后的C代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { public class Pro

我正面临一只奇怪的虫子。 我有大约100个长时间运行的任务,我想同时运行其中的10个

我在这里发现了与我的需求非常相似的东西:在节流部分

这里是简化后的C代码:

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

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            Test();
        }

        public static async void Test()
        {
            var range = Enumerable.Range(1, 100).ToList();

            const int CONCURRENCY_LEVEL = 10;
            int nextIndex = 0;
            var matrixTasks = new List<Task>();

            while (nextIndex < CONCURRENCY_LEVEL && nextIndex < range.Count())
            {
                int index = nextIndex;
                matrixTasks.Add(Task.Factory.StartNew(() => ComputePieceOfMatrix()));
                nextIndex++;
            }

            while (matrixTasks.Count > 0)
            {
                try
                {
                    var imageTask = await Task.WhenAny(matrixTasks);
                    matrixTasks.Remove(imageTask);
                }
                catch (Exception e)
                {
                    Console.Write(1);
                    throw;
                }

                if (nextIndex < range.Count())
                {
                    int index = nextIndex;
                    matrixTasks.Add(Task.Factory.StartNew(() => ComputePieceOfMatrix()));
                    nextIndex++;
                }
            }

            await Task.WhenAll(matrixTasks); 
        }

        private static void ComputePieceOfMatrix()
        {
            try
            {
                for (int j = 0; j < 10000000000; j++) ;
            }
            catch (Exception e)
            {
                Console.Write(2);
                throw;
            }
        }
    }
}

但是它是完全相同的。

Test()
的返回类型更改为
Task
,然后等待
Task
在程序结束之前完成

static void Main(string[] args)
{
    Test().Wait();
}

public static async Task Test()
{
    // ...
}

我会将您的测试从一个void更改为一个任务返回类型,并且在main方法中,我会代替Test()执行该操作


1.您的代码导致异常

try
{
    for (int j = 0; j < 10000000000; j++) ;
}
catch (Exception e)
{
    Console.Write(2);
    throw;
}
试试看
{
对于(int j=0;j<1000000000;j++);
}
捕获(例外e)
{
控制台。写入(2);
投掷;
}
只是一个简单的OverflowException,因为1000000000是长的,并且j计数器int

2.在子线程运行完成之前,主线程已退出。很可能是因为线程在运行时关闭,所以您得到了ThreadAbortException


3.Wait Test()-正确地调用Test(),然后等待任务。如果没有Wait,那又是什么<代码>异步无效?我想你的意思是
异步任务
。Resharper告诉我,如果我不使用任务,返回任务是没有用的。我将尝试使用它。我猜您的主线程在
Test
完成之前退出,而现有线程在应用程序退出之前被中止。我已编辑了我的问题。那不行。。。谢谢哇!是的,你的测试代码是对的。但在我的真实代码中,原因不同。。。我实际上是在给一个网络服务打电话。现在,我必须找到一个小的代码崩溃就像我真正的代码崩溃,但没有像那样愚蠢的错误。。。非常感谢。是的,var imageTask=wait Task.WhenAny(矩阵任务);等待在那里是不正确的。也不要在这里等待,因为您的执行流将被破坏。它必须被破坏,否则我将同时运行10个以上的任务。
Task t = Test();
t.Wait();
try
{
    for (int j = 0; j < 10000000000; j++) ;
}
catch (Exception e)
{
    Console.Write(2);
    throw;
}