假设我有10N项(我需要通过http协议获取它们),在代码中N任务开始获取数据,每个任务按顺序获取10个项目。我将这些项目放在ConcurrentQueue<Item>
中。之后,逐个线程不安全地处理项目。
async Task<Item> GetItemAsync()
{
//fetch one item from the internet
}
async Task DoWork()
{
var tasks = new List<Task>();
var items = new ConcurrentQueue<Item>();
var handles = new List<ManualResetEvent>();
for i 1 -> N
{
var handle = new ManualResetEvent(false);
handles.Add(handle);
tasks.Add(Task.Factory.StartNew(async delegate
{
for j 1 -> 10
{
var item = await GetItemAsync();
items.Enqueue(item);
}
handle.Set();
});
}
//begin to process the items when any handle is set
WaitHandle.WaitAny(handles);
while(true)
{
if (all handles are set && items collection is empty) //***
break;
//in another word: all tasks are really completed
while(items.TryDequeue(out item))
{
AThreadUnsafeMethod(item); //process items one by one
}
}
}
我不知道如果条件可以放在标有***
的语句中。我不能在这里使用Task.IsCompleted
属性,因为我在任务中使用await
,因此任务很快就会完成。并且指示任务是否执行到最后的bool[]
看起来非常难看,因为我认为ManualResetEvent可以做同样的工作。任何人都可以给我一个建议吗?
答案 0 :(得分:3)
嗯,你可以自己构建这个,但我认为使用TPL Dataflow会更轻松。
类似的东西:
static async Task DoWork()
{
// By default, ActionBlock uses MaxDegreeOfParallelism == 1,
// so AThreadUnsafeMethod is not called in parallel.
var block = new ActionBlock<Item>(AThreadUnsafeMethod);
// Start off N tasks, each asynchronously acquiring 10 items.
// Each item is sent to the block as it is received.
var tasks = Enumerable.Range(0, N).Select(Task.Run(
async () =>
{
for (int i = 0; i != 10; ++i)
block.Post(await GetItemAsync());
})).ToArray();
// Complete the block when all tasks have completed.
Task.WhenAll(tasks).ContinueWith(_ => { block.Complete(); });
// Wait for the block to complete.
await block.Completion;
}
答案 1 :(得分:1)
您可以使用超时为零的WaitOne来检查状态。这样的事情应该有效:
if (handles.All(handle => handle.WaitOne(TimeSpan.Zero)) && !items.Any())
break;
答案 2 :(得分:0)
谢谢大家。最后我发现CountDownEvent非常适合这种情况。一般实现如下所示:(对于其他人的信息)
for i 1 -> N
{
//start N tasks
//invoke CountDownEvent.Signal() at the end of each task
}
//see if CountDownEvent.IsSet here