コード例 #1
0
        private static ThreadContext DoCreate(string name, Action <CancellationToken> action, bool isBackground)
        {
            CancellationTokenSource source = new CancellationTokenSource();
            CancellationToken       token  = source.Token;
            var threadResult = new ThreadResult();
            var thread       = new Thread(() => ThreadAction(name, threadResult, action, token));

            thread.Name         = name;
            thread.IsBackground = isBackground;
            return(new ThreadContext(thread, source, threadResult));
        }
コード例 #2
0
ファイル: ThreadCreator.cs プロジェクト: Amir-799/XOutput
        public static ThreadContext Create(string name, Func <CancellationToken, Task> action, bool isBackground = true)
        {
            CancellationTokenSource source = new CancellationTokenSource();
            CancellationToken       token  = source.Token;
            var threadResult = new ThreadResult();
            var thread       = new Thread(() => ThreadAction(name, threadResult, (t) => action(t).Wait(), token));

            thread.Name         = name;
            thread.IsBackground = isBackground;
            return(new ThreadContext(thread, source, threadResult));
        }
コード例 #3
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));
        }
コード例 #4
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.");
 }
コード例 #5
0
ファイル: ThreadContext.cs プロジェクト: yy520a/XOutput
 internal ThreadContext(Thread thread, CancellationTokenSource tokenSource, ThreadResult result)
 {
     this.thread      = thread;
     this.tokenSource = tokenSource;
     this.result      = result;
 }