我是这个线程的初学者,已经两天撞墙了。我到处都读过Parallel.ForeEach会等到线程完成后再从ForEach中出来。我在这里告诉你那是一群你知道的东西。这只会按计划设置一堆任务,并退出ForEach。
所以我的问题是,如何使任务从计划的运行开始?在完成所有线程之前,我该如何保留在程序中?我在下面的这个示例中,它将只计划任务并退出程序。
顺便说一句,我从另一个StackOverflow帖子中偷走了加密部分,只是为了举例说明一个需要花费一些时间的任务。
编辑-建议我包含对粘贴的加密代码的引用,这是一个好主意。我相信它最初来自Microsoft,但他们的答案在这里(https://stackoverflow.com/a/273499/1352794)。
编辑v2-我一直在玩这些游戏,并在这里使用帮助(谢谢),我想出了
Parallel.ForEach(tasks, new ParallelOptions { MaxDegreeOfParallelism = 4 }, task =>
{
task.Start();
task.Wait();
Console.WriteLine(task.Status);
});
看起来“开始”和“等待”对此非常重要。使用这两个命令,它现在可以工作。我正在尝试使其运行在最多4个线程上,并且我很难确保确实如此。有什么建议么?我也希望这是处理一堆任务并将其推送到多个线程的正确方法。
class Program
{
static void Main(string[] args)
{
List<Task> tasks = new List<Task>();
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
tasks.Add(new System.Threading.Tasks.Task(() => DoEncryption()));
bool testbool = Parallel.ForEach(tasks, new ParallelOptions { MaxDegreeOfParallelism = 4 }, myTest =>
{
System.Threading.Tasks.Task.Run(() => myTest);
Console.WriteLine(myTest.Status);
}).IsCompleted;
Console.WriteLine("The parallel status is " + testbool);
string mymessage = System.Diagnostics.Process.GetCurrentProcess().Threads.Count.ToString();
}
private static void DoEncryption()
{
try
{
string original = "Here is some data to encrypt!";
// Create a new instance of the RijndaelManaged
// class. This generates a new key and initialization
// vector (IV).
using (RijndaelManaged myRijndael = new RijndaelManaged())
{
myRijndael.GenerateKey();
myRijndael.GenerateIV();
// Encrypt the string to an array of bytes.
byte[] encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);
//Display the original data and the decrypted data.
Console.WriteLine("Original: {0}", original);
}
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
}
}
static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;
// Create an RijndaelManaged object
// with the specified key and IV.
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Key = Key;
rijAlg.IV = IV;
// Create a decryptor to perform the stream transform.
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
}
答案 0 :(得分:4)
您想得太多了。
您可以通过任务执行此操作
var tasks = new List<Task>();
tasks.Add(new Task(DoEncryption));
tasks.Add(new Task(DoEncryption));
tasks.Add(new Task(DoEncryption));
tasks.Add(new Task(DoEncryption));
tasks.Add(new Task(DoEncryption));
Task.WhenAll(tasks);
或Parallel.For
var options = new ParallelOptions
{
MaxDegreeOfParallelism = 4
};
Parallel.For(0, 10, options, i => DoEncryption());
或者,如果您稍微更改DoEncryption
,则可以使用Parallel.ForEach
private static void DoEncryption2(string original)
{
try
{
using (var myRijndael = new RijndaelManaged())
{
myRijndael.GenerateKey();
myRijndael.GenerateIV();
// Encrypt the string to an array of bytes.
var encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);
//Display the original data and the decrypted data.
Console.WriteLine("Original: {0}, Encrypted {1}", original, string.Join("", encrypted.Select(b => b.ToString("X2"))));
}
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e.Message);
}
}
用法
var list = new List<string>
{
"Here is some hjdata to encrypt!",
"Here is sghjome data to encrypt!",
"Here is somfghe data to encrypt!",
"Here is somfghe data to encrypt!",
"Here is sohjkme data to encrypt!",
"Here is somghje data to encrypt!",
"Here is somfgheh data to encrypt!",
"Here is somefgh data to encrypt!",
"Here is sfghfgome data to encrypt!",
"Here is somefghfgh data to encrypt!",
"Here is shome data to encrypt!",
"Here is some data to encrypt!",
"Here is sfghfghfghome data to encrypt!",
"Here is some data to encrypt!",
"Here is somfghe data to encrypt!"
};
Parallel.ForEach(list, options, DoEncryption2);
输出
Original: Here is somfgheh data to encrypt!
Encrypted 2A 9F 25 EA 4E D2 0E BB 0D 46 43 04 F5 B6 63 7F 08 E6 62 A3 64 C3 97 D4 4A D6 F0 1F DA E3 26 54 E0 E7 1B EF EB C1 68 8F 3C DB 52 58 1F F9 A9 20
Original: Here is somfghe data to encrypt!
Encrypted CD 04 A7 8F BB 1D AB E1 D4 91 2D 09 D3 91 62 7A 2F 55 E7 06 52 C6 44 18 06 D1 5F 4F 59 56 66 65 33 E4 2E 15 55 CF 36 75 80 B0 52 F4 85 81 F0 A9
Original: Here is somefghfgh data to encrypt!
Encrypted 5A E6 37 A5 F4 D4 92 5B 75 F2 22 3F 46 F9 C5 8B 5C 56 ED 78 1C B2 2E AE 16 5B F8 22 6E 40 55 A2 D7 FD FD 8C 34 65 22 06 9A C5 73 30 BC AF CB A2
Original: Here is sohjkme data to encrypt!
Encrypted D3 77 FA D1 31 18 DF 4C CD B1 9D 17 56 EF 68 F8 FB 02 D5 AD F6 47 80 27 8C 77 78 A0 AF 6C E4 42 19 39 C3 F6 4C 5D 19 CB CB 8E B9 A8 C1 B6 AA 71
Original: Here is somefgh data to encrypt!
Encrypted BD E4 C4 33 D9 65 EF 47 BF 8D 93 EE EB EB 45 BF 6F BE A8 7B B0 CB AA 74 CD 4B 4A FD A5 26 06 DD 4D 52 E7 BC 4F B0 4F 99 94 BE 64 38 6F E5 04 35
Original: Here is shome data to encrypt!
Encrypted 18 40 3C 30 FD 1D D5 C7 A2 29 2D 3F 3A 54 0A 06 97 70 97 F6 86 9B 7B 1F 64 C2 83 1E 13 71 92 CB
Original: Here is somghje data to encrypt!
Encrypted 84 FA EB 84 46 D7 E2 1A 8F FB 3C 48 94 3C A7 4E 03 3D B8 C1 28 F5 AE 8B FD 64 4F 7E 12 2D D8 DE 12 3F 08 AD 10 50 C8 51 88 04 A1 FF 10 58 B6 D5
Original: Here is sfghfgome data to encrypt!
Encrypted 92 47 76 2A 3C C1 77 EE 03 CB 91 E2 B1 42 1D 21 C9 EE A2 57 CB A4 A9 31 60 21 C7 A4 CD F1 9C 5F 6B 8C 44 23 5F 1A 1F 44 74 D9 EA 3B DF 15 8A 9A
Original: Here is some data to encrypt!
Encrypted 6D 50 00 68 04 F4 FD EC 08 21 76 27 7D 66 DA 33 B2 7B DD CC B5 7D 24 8B 44 B7 AC 2D E7 DB B6 40
Original: Here is sfghfghfghome data to encrypt!
Encrypted F2 FA 09 62 94 F5 78 A1 07 5E D1 DF 9D 09 B2 B4 DD 67 B8 A6 15 A5 21 7C 06 59 1A CD 9A 2E 94 5C 73 5E 9C F2 13 FE 88 36 74 B1 64 B7 57 F9 FC 70
Original: Here is sghjome data to encrypt!
Encrypted 57 D5 3C E2 CC F0 E9 02 E3 40 AC 43 F4 22 E5 44 A9 B2 A1 37 5D 42 17 6B 11 B6 1D 70 C9 CE 64 6B 85 36 56 9E 3C CF 6D 94 71 F9 28 15 7D 78 F9 5C
Original: Here is some data to encrypt!
Encrypted 7E BD BC 34 23 42 27 82 6A 52 D7 4B 4C AD 1C FC 55 7A 7E AF A1 34 24 25 B7 F7 C2 F1 9F 25 49 D1
Original: Here is somfghe data to encrypt!
Encrypted C3 6A A7 CD 43 76 FD CB 4C 6E 29 A6 96 C4 AA E0 94 7C 4F D1 99 9F 86 AF ED A8 5C C7 DC 15 BD BA C2 21 F9 C2 09 1C 64 BB 3D 4C 0E 41 E6 C9 2E F3
Original: Here is somfghe data to encrypt!
Encrypted 87 CD 1A 35 08 42 02 2A EC 70 6C 2D CC 75 66 88 8C 5E 9A DC 5B AC 72 D7 26 9A F0 11 76 C1 C4 C7 C9 D8 36 7B 77 89 AB 45 39 60 50 D6 88 63 35 F1
Original: Here is some hjdata to encrypt!
Encrypted 3C AC F0 69 9B 2C E4 54 4A 7B 78 5F F4 D9 AD D3 42 E7 90 5E 8F CF 02 1F 13 C2 BF 9E B4 94 76 08