gpt4 book ai didi

C#相当于Objective-C的dispatch_group和queue?

转载 作者:行者123 更新时间:2023-11-30 22:02:31 27 4
gpt4 key购买 nike

我正在将一个 Objective-C 应用程序移植到 C#,我正在努力在 C# 中实现以下功能

// create a new dispatch group
dispatch_group_t dispatchGroup = dispatch_group_create();

dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// iterate over a collection adding blocks to the dispatchQueue identified by dispatchGroup
dispatch_group_async(dispatchGroup, dispatchQueue, ^{
// do stuff
});
}

// wait for them all to finish
dispatch_group_wait(dispatchGroup, DISPATCH_TIME_FOREVER);

// do more stuff that depends on them all finishing

我将如何做这样的事情?

最佳答案

注意:这至少需要 .NET Framework 4.5 版。

// create a new dispatch group
List<Task> tasks = new List<Task>();

// iterate over a collection adding tasks to the dispatch group
for (…) {
Task task = Task.Run(() => {
// do stuff
});
tasks.Add(task);
}

// wait for them all to finish
Task.WaitAll(tasks.ToArray());

// do more stuff that depends on them all finishing

关于C#相当于Objective-C的dispatch_group和queue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26823155/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com