Beispiel #1
0
 public void TaskManager_WithCancellation_Test()
 {
    // create manager and add tasks
    var manager = new TaskManager();
    manager.Changed += (s, e) => ReportAction(e);
    manager.Add("task1", ct => Thread.Sleep(500), 100);
    manager.Add("task2", ct => Thread.Sleep(500), 50);
    // stagger start tasks
    manager.Start("task1");
    // schedule a cancellation in 2.5 to 5 seconds
    var cancelTask = Task.Factory.StartNew(() =>
    {
       var random = new Random();
       Thread.Sleep(random.Next(2500, 5000));
       manager.Cancel();
    });
    Thread.Sleep(2000);
    manager.Start("task2");
    Thread.Sleep(3000);
    // stop tasks
    manager.Stop();
    // wait for completion (only in unit tests)
    manager.Wait();
    // wait for completion of cancellation task
    cancelTask.Wait();
 }
Beispiel #2
0
 public void TaskManager_Test()
 {
    // create manager and add tasks
    var manager = new TaskManager();
    manager.Changed += (s, e) => ReportAction(e);
    manager.Add("task1", ct => Thread.Sleep(500), 100);
    manager.Add("task2", ct => Thread.Sleep(500), 50);
    // stagger start tasks
    manager.Start("task1");
    Thread.Sleep(2000);
    manager.Start("task2");
    Thread.Sleep(3000);
    // stop tasks
    manager.Stop();
    // wait for completion (only in unit tests)
    manager.Wait();
 }