using System; using System.Threading; using System.Threading.Tasks; using System.Linq; namespace SBRL.Utilities { class AsyncTools { /// /// Call this method to allow a given task to complete in the background. /// Errors will be handled correctly. /// Useful in fire-and-forget scenarios, like a TCP server for example. /// From http://stackoverflow.com/a/22864616/1460422 /// /// The task to forget about. /// Acceptable exceptions. Exceptions specified here won't cause a crash. public static async void ForgetTask(Task targetTask, params Type[] acceptableExceptions) { try { await targetTask.ConfigureAwait(false); } catch (Exception ex) { // TODO: consider whether derived types are also acceptable. if (!acceptableExceptions.Contains(ex.GetType())) throw; } } } }