public static Thread CreateAndStart(ThreadStartParameters parameters) { Thread thread = Create(parameters); thread.Start(); return(thread); }
public static Thread Create(ThreadStartParameters parameters) { if (parameters.Task == null) { throw new ArgumentException(nameof(parameters)); } var thread = new Thread(() => { logger.Debug(() => $"Thread {parameters.Name} is started."); try { parameters.Task(); } catch (ThreadAbortException) { logger.Debug(() => $"Thread {parameters.Name} is aborted."); } catch (ThreadInterruptedException) { logger.Debug(() => $"Thread {parameters.Name} is interrupted."); } catch (Exception e) { logger.Error($"Thread {parameters.Name} failed with error.", e); parameters.Error?.Invoke(e); } finally { parameters.Finally?.Invoke(); } logger.Debug(() => $"Thread {parameters.Name} is stopped."); }); if (parameters.Name != null) { thread.Name = parameters.Name; } thread.IsBackground = parameters.IsBackground; return(thread); }