Пример #1
0
        private static ThreadContext DoCreate(string name, Action <CancellationToken> action, bool isBackground, CancellationToken token = default)
        {
            CancellationTokenSource source       = new CancellationTokenSource();
            CancellationTokenSource linkedTokens = CancellationTokenSource.CreateLinkedTokenSource(source.Token, token);
            var threadResult = new ThreadResult();
            var thread       = new Thread(() => ThreadAction(name, threadResult, action, linkedTokens.Token))
            {
                Name         = name,
                IsBackground = isBackground
            };

            return(new ThreadContext(thread, source, threadResult));
        }
Пример #2
0
 private static void ThreadAction(string name, ThreadResult threadResult, Action <CancellationToken> action, CancellationToken token)
 {
     logger.Debug(() => $"Thread {name} is started.");
     try
     {
         action.Invoke(token);
     }
     catch (Exception e)
     {
         if (!token.IsCancellationRequested)
         {
             logger.Error(e, $"Thread {name} failed with error.");
             threadResult.Error = e;
         }
     }
     logger.Debug(() => $"Thread {name} is stopped.");
 }
Пример #3
0
 internal ThreadContext(Thread thread, CancellationTokenSource tokenSource, ThreadResult result)
 {
     this.thread      = thread;
     this.tokenSource = tokenSource;
     this.result      = result;
 }