首页 > 解决方案 > .NET 2.0 中的等效任务是什么?

问题描述

我知道 .NET 2.0 现在真的很老了,但我有代码要在 .NET 2.0 下编译。不幸的是,源代码使用 .NET 4.0 System.Threading.Tasks

这是代码:

 public override string Encode(byte[] data)
 {
     if (data == null || data.Length == 0)
         return "";

     int mainBitsLength = (data.Length * 8 / BlockBitsCount) * BlockBitsCount;
     int tailBitsLength = data.Length * 8 - mainBitsLength;
     int totalBitsLength = mainBitsLength + tailBitsLength;
     int mainCharsCount = mainBitsLength * BlockCharsCount / BlockBitsCount;
     int tailCharsCount = (tailBitsLength * BlockCharsCount + BlockBitsCount - 1) / BlockBitsCount;
     int totalCharsCount = mainCharsCount + tailCharsCount;
     int iterationCount = mainCharsCount / BlockCharsCount;

     var result = new char[totalCharsCount];

     if (!Parallel)
         EncodeBlock(data, result, 0, iterationCount);
     else {
         int processorCount = Math.Min(iterationCount, Environment.ProcessorCount);
         System.Threading.Tasks.Parallel.For(0, processorCount, i =>
            {
                int beginInd = i * iterationCount / processorCount;
                int endInd = (i + 1) * iterationCount / processorCount;
                EncodeBlock(data, result, beginInd, endInd);
            });
     }

     if (tailBitsLength != 0)
     {
         ulong bits = GetBits64(data, mainBitsLength, tailBitsLength);
         BitsToChars(result, mainCharsCount, tailCharsCount, bits);
     }

     return new string(result);
 }

我不是线程专家。有人可以在 .NET 2.0 中重现相同的行为以及清晰的解释吗?

标签: c#.net-2.0

解决方案


推荐阅读