- Instead of calling
var task = Task.Factory.StartNew(runTask);or like Bruno Costa correctly suggested with aCancellationTokenlikevar task = Task.Factory.StartNew(runTask, cancellationToken);you can here use for NET 4.5 the overloadedTask.Run(Action, CancellationToken)method, which can be seen as a simplified call toTask.Factory.StartNew().
See also : http://stackoverflow.com/a/22087211https://stackoverflow.com/a/22087211
By providing a default value for
taskNamethe message of a thrownTaskTimeoutExceptionwould look better.Early checking if
TimeSpan timeoutis in the given range, will reduce overhead.Action runTaskshould be renamed torunActionor better to justaction.