Created
April 19, 2012 13:24
-
-
Save chgeuer/2420964 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication3 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var cts = new CancellationTokenSource(); | |
var ct = cts.Token; | |
var tasks = new List<Task>(); | |
Action bumm = () => | |
{ | |
var process = Process.Start(@"c:\Windows\System32\cmd.exe", "/k"); | |
if (process == null) throw new NotSupportedException(); | |
while (true) | |
{ | |
if (ct.WaitHandle.WaitOne(TimeSpan.FromSeconds(1))) | |
{ | |
process.Kill(); | |
return; | |
} | |
} | |
}; | |
var t = new Task(bumm, ct); | |
t.Start(); | |
tasks.Add(t); | |
tasks.Add(Task.Factory.StartNew(bumm, ct)); | |
try | |
{ | |
Console.ReadKey(); | |
cts.Cancel(); | |
Task.WaitAll(tasks.ToArray()); | |
} | |
catch (AggregateException ae) | |
{ | |
foreach (Exception e in ae.InnerExceptions) | |
Console.Error.WriteLine(e.Message); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment