c# - .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 中重现相同的行为以及清晰的解释吗?
解决方案
推荐阅读
- python - 如何通过 BeautifulSoup 打印波斯语页面的文本?
- flutter - Flutter:当前 Flutter SDK 版本为 0.0.0-未知
- google-colaboratory - /bin/bash: ./darknet: No such file or directory 问题
- java - 为什么在迭代 HashMap 时在 IF 块中添加元素会起作用?
- arrays - 如何检查数组中元素的位置?
- apache-kafka - KTable-KTable 外键连接语义
- ansible - Ansible 用户模块:如何将 UID 定义为默认设置?
- sqlite - 按时间戳分组 sqlite3
- c++ - c++中动态内存分配的问题
- python - 压缩包含所有内容的文件夹,而不保留 Python 中的目录结构