예제 #1
0
        public static Thread CreateAndStart(ThreadStartParameters parameters)
        {
            Thread thread = Create(parameters);

            thread.Start();
            return(thread);
        }
예제 #2
0
        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);
        }