【发布时间】:2017-05-15 04:22:58
【问题描述】:
我有一个接受值的方法,如果满足条件,则该操作不应运行 24 小时。但是当它停止时,我想运行其他不满足该条件的线程。
在这个例子中,我在程序开始时创建了 30 个线程。一旦我做了 5 块奶酪,我就需要停下来,因为那太多了。如果有一个地方可以发送在其他线程正在运行时直到时间用完才能执行的线程,那就太好了。 Task.Delay 即使使用 Wait 在这里似乎也不起作用。
这是我的代码示例:
//Stop making cheese when you have enough for the day but continue making others
public void madeEnoughToday(string cheese)
{
//Find how much cheese is made based on cheese type.
DataGridViewRow row = cheeseGV.Rows
.Cast<DataGridViewRow>()
.Where(r =>
r.Cells["Cheese"].Value.ToString().Equals(cheese))
.First();
if (row.Cells["MadeToday"].Value.Equals(row.Cells["Perday"].Value))
{
Task.Delay(30000).Wait();
}
}
【问题讨论】:
标签: multithreading asynchronous synchronization locking semaphore