I need to create a program that counts to 10 using two threads.
one threads should print even numbers and the other should print odd numbers.
These threads should print the numbers in order (1, 2, 3, 4, 5...)
I have done this code but it seems not to work... any ideas?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
private static AutoResetEvent autoEvent;
private static int num = 10;
static void DoWork(object data)
{
var counter = (int)data;
if (counter == 1)
{
autoEvent.WaitOne();
}
while (counter <= num)
{
Console.WriteLine(counter);
counter += 2;
autoEvent.Set();
autoEvent.WaitOne();
}
}
static void Main(string[] args)
{
autoEvent = new AutoResetEvent(false);
var evenCounter = new Thread(DoWork);
evenCounter.Start(0);
var notEvenCounter = new Thread(DoWork);
notEvenCounter.Start(1);
Console.ReadLine();
}
}
}