var urls = new ConcurrentQueue<string>(Directory.GetFiles(@"C:\Windows\System32"));
var total = urls.Count;
var tasks = new Task[20];
for (int i = 0; i < tasks.Length; i++)
{
tasks[i] = new Task(() =>
{
string url;
while (urls.TryDequeue(out url))
{
Console.WriteLine(url);
Thread.
Sleep(1000 + rand.
Next(4000)); }
});
}
var prog = Task.Factory.StartNew(() =>
{
foreach (var t in tasks)
{
t.Start();
}
while (!Task.WaitAll(tasks, 1000))
{
Console.WriteLine(100 * urls.Count / total);
}
});
prog.Wait();
ICAgICAgICAgICAgdmFyIHVybHMgPSBuZXcgQ29uY3VycmVudFF1ZXVlPHN0cmluZz4oRGlyZWN0b3J5LkdldEZpbGVzKEAiQzpcV2luZG93c1xTeXN0ZW0zMiIpKTsKICAgICAgICAgICAgdmFyIHRvdGFsID0gdXJscy5Db3VudDsKICAgICAgICAgICAgdmFyIHRhc2tzID0gbmV3IFRhc2tbMjBdOwogICAgICAgICAgICB2YXIgcmFuZCA9IG5ldyBSYW5kb20oKTsKCiAgICAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgdGFza3MuTGVuZ3RoOyBpKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHRhc2tzW2ldID0gbmV3IFRhc2soKCkgPT4KICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBzdHJpbmcgdXJsOwoKICAgICAgICAgICAgICAgICAgICB3aGlsZSAodXJscy5UcnlEZXF1ZXVlKG91dCB1cmwpKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUodXJsKTsKICAgICAgICAgICAgICAgICAgICAgICAgVGhyZWFkLlNsZWVwKDEwMDAgKyByYW5kLk5leHQoNDAwMCkpOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0pOwogICAgICAgICAgICB9CgogICAgICAgICAgICB2YXIgcHJvZyA9IFRhc2suRmFjdG9yeS5TdGFydE5ldygoKSA9PgogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBmb3JlYWNoICh2YXIgdCBpbiB0YXNrcykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICB0LlN0YXJ0KCk7CiAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICAgICAgd2hpbGUgKCFUYXNrLldhaXRBbGwodGFza3MsIDEwMDApKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKDEwMCAqIHVybHMuQ291bnQgLyB0b3RhbCk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0pOwoKICAgICAgICAgICAgcHJvZy5XYWl0KCk7