提问人:김영호 提问时间:8/8/2019 最后编辑:halfer김영호 更新时间:8/9/2019 访问量:107
.NET 2.0 中的等效任务是什么?
What is the equivalent task in .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 中重现相同的行为并给出明确的解释吗?
答: 暂无答案
评论
System.Threading.Thread
Parallel