C# 循环C中的任务等待#
我正在读一个文件,想一次处理10行 这仅在处理<10行时有效。For循环只是继续,并不等待前10个任务完成 请告知 下面是我的代码:C# 循环C中的任务等待#,c#,asynchronous,foreach,task,wait,C#,Asynchronous,Foreach,Task,Wait,我正在读一个文件,想一次处理10行 这仅在处理0) Task.WaitAll(tasks.ToArray()); } 当你说“请建议”时,我有很大的自由去尝试和帮助。以下是我的建议,我希望你觉得这些建议“有用”: 问:“我正在读一个文件,想一次处理10行。” 答:在生成任何任务之前,将测试列表分成10个字符串的组(如果列表长度为25个字符串,则最后一个组可能是部分的) 以下是执行第一部分的方法: // Short-running method that queues up strings
private void btnStart_Click(object sender, EventArgs e)
{
int lctr = 0;
var tasks = new List<Task>();
foreach (string s in lines)
{
if (string.IsNullOrWhiteSpace(s) || string.IsNullOrEmpty(s))
continue;
lctr++;
tasks = new List<Task>();
if (lctr < 11)
{
Console.WriteLine(lctr + " - " + s);
tasks.Add(Task.Factory.StartNew(() => processThis(s)));
}
else
{
Console.WriteLine("Waiting ...");
Task.WaitAll(tasks.ToArray());
}
}
if (tasks.Count > 0)
Task.WaitAll(tasks.ToArray());
}
private void btnStart\u单击(对象发送方,事件参数e)
{
int-lctr=0;
var tasks=新列表();
foreach(行中的字符串s)
{
if(string.IsNullOrWhiteSpace(s)| | string.IsNullOrEmpty(s))
继续;
lctr++;
任务=新列表();
如果(lctr<11)
{
控制台写入线(lctr+“-”+s);
tasks.Add(Task.Factory.StartNew(()=>processThis));
}
其他的
{
控制台。WriteLine(“等待…”);
Task.WaitAll(tasks.ToArray());
}
}
如果(tasks.Count>0)
Task.WaitAll(tasks.ToArray());
}
当你说“请建议”时,我有很大的自由去尝试和帮助。以下是我的建议,我希望你觉得这些建议“有用”:
问:“我正在读一个文件,想一次处理10行。”
答:在生成任何任务之前,将测试列表分成10个字符串的组(如果列表长度为25个字符串,则最后一个组可能是部分的)
以下是执行第一部分的方法:
// Short-running method that queues up strings in batches of 10 or less
private static Queue<List<string>> enqueueBatchesOf10orLess(List<string> testData)
{
Queue<List<string>> batchesOfUpTo10 = new Queue<List<string>>();
List<string> singleBatchOfUpTo10 = new List<string>(); ;
for (int count = 0; count < testData.Count; count++)
{
if ((count % 10) == 0)
{
if(count != 0) // Skip the very first time
{
batchesOfUpTo10.Enqueue(singleBatchOfUpTo10);
singleBatchOfUpTo10 = new List<string>();
}
}
singleBatchOfUpTo10.Add(testData[count]);
}
// Leftover batch of less-than-10
if(singleBatchOfUpTo10.Count != 0)
{
batchesOfUpTo10.Enqueue(singleBatchOfUpTo10);
}
return batchesOfUpTo10;
}
要测试这一点:
static void Main(string[] args)
{
// Make some test data
List<string> testData = new List<string>();
for (int i = 0; i < 25; i++) testData.Add((i + 1).ToString());
Queue<List<string>> batchesOf10OrLess = enqueueBatchesOf10orLess(testData);
List<Task> tasks = new List<Task>();
for (int i = 0; i < batchesOf10OrLess.Count; i++)
{
// Pass in the queue of batches and get a Task in return.
tasks.Add(processBatch(batchesOf10OrLess));
}
Console.WriteLine("The test data is NOT in order.");
Console.WriteLine("This is proof the tasks are running concurrently");
Task.WaitAll(tasks.ToArray());
// Pause
Console.ReadKey();
}
如果您喜欢GitHub中的此示例。
任务
在其上等待所有时将始终为空,因为您在If
之前重新为其分配了一个空列表。此外,您还需要在每批10个任务之后重置lctr
,否则您将永远不会再次输入lctr<11
条件。为什么要使用这里有任务吗?什么都没有发生。processThis
async吗?您实际上想做什么?并行处理批次中的10个项目?为什么要在批之间等待?有两个问题:您正在为每个linge重置任务,并且没有将lctr设置为每10行零。感谢您的时间和建议,我将在脱机后的几天内尝试一下。
private static void processThis(string s)
{
Task.Delay(100).Wait(); // Simulate long-running string processing
Console.WriteLine(s);
}
static void Main(string[] args)
{
// Make some test data
List<string> testData = new List<string>();
for (int i = 0; i < 25; i++) testData.Add((i + 1).ToString());
Queue<List<string>> batchesOf10OrLess = enqueueBatchesOf10orLess(testData);
List<Task> tasks = new List<Task>();
for (int i = 0; i < batchesOf10OrLess.Count; i++)
{
// Pass in the queue of batches and get a Task in return.
tasks.Add(processBatch(batchesOf10OrLess));
}
Console.WriteLine("The test data is NOT in order.");
Console.WriteLine("This is proof the tasks are running concurrently");
Task.WaitAll(tasks.ToArray());
// Pause
Console.ReadKey();
}
The test data is NOT in order.
This is proof the tasks are running concurrently
1
21
11
2
12
22
3
23
13
4
14
24
5
25
15
6
16
7
17
8
18
19
9
10
20