Created
May 20, 2015 22:34
-
-
Save yetanotherchris/f34151e48adb024c0997 to your computer and use it in GitHub Desktop.
This console application allows you to start, stop, list and query running tasks. The DoStuff() method does the work itself - 50 iterations of sleeping for 10 seconds.
This file contains 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.Linq; | |
using System.Security.Permissions; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace TplTest | |
{ | |
class Program | |
{ | |
private static List<Task<MyRunner>> tasks = new List<Task<MyRunner>>(); | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Commands: start, list, stop, start {amount}, stop {taskid}, c to quit."); | |
string input = Console.ReadLine(); | |
while (input != "c") | |
{ | |
if (input.StartsWith("start")) | |
{ | |
HandleStartCommand(input); | |
} | |
if (input == "list") | |
{ | |
HandleListCommand(); | |
} | |
if (input.StartsWith("stop")) | |
{ | |
HandleStopCommand(input); | |
} | |
input = Console.ReadLine(); | |
} | |
} | |
private static void HandleStopCommand(string input) | |
{ | |
int taskId = 0; | |
string number = input.Replace("stop", ""); | |
if (!int.TryParse(number, out taskId)) | |
taskId = 0; | |
if (taskId > 0) | |
{ | |
Task<MyRunner> task = tasks.FirstOrDefault(t => t.Id == taskId); | |
if (task != null) | |
{ | |
task.Result.CancelTokenSource.Cancel(false); | |
Console.WriteLine("Attempting to stop task id {0}", taskId); | |
} | |
} | |
else | |
{ | |
Console.WriteLine("Can't parse {0} as a task id", number); | |
} | |
} | |
private static void HandleListCommand() | |
{ | |
foreach (Task<MyRunner> task in tasks) | |
{ | |
Console.WriteLine("{0} {1} {2}", task.Id, task.Result.ChildTask.Status, task.Result.Show()); | |
} | |
} | |
private static void HandleStartCommand(string input) | |
{ | |
int repeat = 1; | |
string count = input.Replace("start", ""); | |
if (!int.TryParse(count, out repeat)) | |
repeat = 1; | |
for (int i = 0; i < repeat; i++) | |
{ | |
CancellationTokenSource cancelSource = new CancellationTokenSource(); | |
CancellationToken cancelToken = cancelSource.Token; | |
MyRunner runner = new MyRunner(Guid.NewGuid(), cancelSource); | |
Task<MyRunner> parentTask = Task.Run(() => | |
{ | |
Task childTask = Task.Run(() => { runner.DoStuff(); }, cancelToken); | |
runner.CancelTokenSource = cancelSource; | |
runner.ChildTask = childTask; | |
return runner; | |
}); | |
tasks.Add(parentTask); | |
} | |
} | |
} | |
public class MyRunner | |
{ | |
public Task ChildTask { get; set; } | |
private readonly Guid _id; | |
public CancellationTokenSource CancelTokenSource { get; set; } | |
private int _step; | |
public MyRunner(Guid id, CancellationTokenSource cancelToken) | |
{ | |
_id = id; | |
CancelTokenSource = cancelToken; | |
} | |
public void DoStuff() | |
{ | |
Console.WriteLine("Running {0}", _id); | |
for (int i = 0; i < 50; i++) | |
{ | |
if (CancelTokenSource.IsCancellationRequested) | |
break; | |
Thread.Sleep(10000); | |
_step++; | |
} | |
} | |
public string Show() | |
{ | |
string cancelMessage = ""; | |
if (CancelTokenSource.Token.IsCancellationRequested) | |
{ | |
cancelMessage = "(cancelled)"; | |
} | |
return string.Format("{0} - I'm at, or got to step {1} {2}", _id, _step, cancelMessage); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment