public async Task N2() { var o = new ZeroEvenOdd(2); var s = ""; var tasks = new[] { Task.Factory.StartNew(() => { o.Even((i) => s += i); }), Task.Factory.StartNew(() => { o.Zero((i) => s += i); }), Task.Factory.StartNew(() => { o.Odd((i) => s += i); }) }; var delay = Task.Delay(timeout); var result = await Task.WhenAny(Task.WhenAll(tasks), delay); if (delay == result) { Assert.Fail("Timeout"); } Assert.AreEqual("0102", s); }
public void DisplayZeroEvenOdd() { ZeroEvenOdd a = new ZeroEvenOdd(10); Task.Run(() => a.Zero(p => Console.WriteLine(p.ToString()))); Task.Run(() => a.Even(p => Console.WriteLine(p.ToString()))); Task.Run(() => a.Odd(p => Console.WriteLine(p.ToString()))); }
public void RunProblem() { var oddTemp = new ZeroEvenOdd(2); Action a1 = () => oddTemp.Zero((i) => Console.WriteLine($"{i}")); Action a2 = () => oddTemp.Even((i) => Console.WriteLine($"{i}")); Action a3 = () => oddTemp.Odd((i) => Console.WriteLine($"{i}")); Task.Run(a1); Task.Run(a2); Task.Run(a3); Console.ReadKey(); }
public void Check_ZeroEvenOdd_BaseCase(int n, string result) { string wholeString = string.Empty; var zeroEvenOdd = new ZeroEvenOdd(n); var zeroAction = new Action <int>((x) => wholeString = string.Concat(wholeString, x.ToString())); var evenAction = new Action <int>((x) => wholeString = string.Concat(wholeString, x.ToString())); var oddAction = new Action <int>((x) => wholeString = string.Concat(wholeString, x.ToString())); Parallel.Invoke ( () => zeroEvenOdd.Zero(zeroAction), () => zeroEvenOdd.Even(evenAction), () => zeroEvenOdd.Odd(oddAction) ); Assert.AreEqual(result, wholeString); }
public async Task N555() { var c = 555; var o = new ZeroEvenOdd(c); var s = ""; var tasks = new[] { Task.Factory.StartNew(() => { o.Even((i) => { s += i; }); }), Task.Factory.StartNew(() => { o.Zero((i) => { s += i; }); }), Task.Factory.StartNew(() => { o.Odd((i) => { s += i; }); }) }; var delay = Task.Delay(timeout); var result = await Task.WhenAny(Task.WhenAll(tasks), delay); var e = string.Join("", Enumerable.Range(1, c).Select(i => $"0{i}")); if (delay == result) { Assert.Fail("Timeout"); } Assert.AreEqual(e, s); }