static void Main(string[] args)
{
int taskCount = 6;
var tasks = new Task[taskCount];
var autoResetEvent = new AutoResetEvent(false);
for (int i = 0; i < taskCount; i++)
{
tasks[i] = Task.Factory.StartNew(state =>
{
var index = (int)state;
if (index < 3)
{
tasks[(index + 1) % 3].Wait();
}
else if (index == 3)
{
Task.Factory.StartNew(() =>
{
while (true) { }
}).Wait();
}
else
{
autoResetEvent.WaitOne();
}
}, i);
}
Task.WaitAll(tasks);
autoResetEvent.Set();
}
What a mess – I have three tasks waiting on each other (deadlocked), two more tasks waiting indefinitely on a AutoResetEvent and one task that starts yet another task that runs an infinite loop (just for fun).Labels: .NET, Debug, Tasks, Visual Studio