|
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
-
-
- namespace DataVAPI.Tool
- {
- /// <summary>
- /// 线程集中处理委托
- /// </summary>
- /// <param name="action"></param>
- public delegate void ActionKeyHandle(string action);
- public delegate void ThreadExceptionHandle(string action, Exception ex);
- public delegate void ThreadExitHandle(string action);
- /// <summary>
- /// 执行器
- /// </summary>
- public class Executer : Singleton<Executer>
- {
- private System.Timers.Timer _timer4Monitor = new System.Timers.Timer(1000);
- private ConcurrentDictionary<string, Thread> _actions = new ConcurrentDictionary<string, Thread>();
- private object _async = new object();
- public event ThreadExceptionHandle OnThreadException;
- public event ThreadExitHandle ThreadExit;
- /// <summary>
- /// 构造器
- /// </summary>
- public Executer()
- {
- }
-
- /// <summary>
- ///
- /// </summary>
- public void Dispose()
- {
- _timer4Monitor.Stop();
- foreach (var th in _actions)
- {
- try { th.Value.Abort(); }
- catch (ThreadAbortException) { }
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- public List<Thread> GetActionInfos()
- {
- Monitor.TryEnter(_async, 200);
- var actionInfos = _actions.Values.ToList();
- Monitor.Exit(_async);
- return actionInfos;
- }
-
-
- /// <summary>
- ///
- /// </summary>
- /// <param name="key"></param>
- public void Abort(string key)
- {
- Monitor.TryEnter(_async, 200);
- if (_actions.ContainsKey(key))
- {
- try { _actions[key].Abort(); }
- catch (ThreadAbortException) { }
- }
- Monitor.Exit(_async);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public bool ContainsKey(string key)
- {
- Monitor.TryEnter(_async, 200);
- var item = _actions[key];
- Monitor.Exit(_async);
- if (null != item)
- {
- return true;
- }
- return false;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="action"></param>
- /// <param name="name"></param>
- /// <param name="isBackground"></param>
- /// <param name="priority"></param>
- public void Start(Action action, string name, bool isBackground = false, ThreadPriority priority = ThreadPriority.Normal)
- {
- Thread thread = new Thread(() =>
- {
- try
- {
- action();
- ThreadExit?.Invoke(name);
- }
- catch (Exception ex)
- {
- OnThreadException?.Invoke(name, ex);
- }
- });
- thread.IsBackground = isBackground;
- thread.Priority = priority;
- thread.Name = name;
- thread.Start();
- Monitor.TryEnter(_async, 50);
- if (_actions.ContainsKey(name))
- {
- try { _actions[name].Abort(); }
- catch (ThreadAbortException) { }
- }
- _actions[name] = thread;
- Monitor.Exit(_async);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="action"></param>
- /// <param name="timeout"></param>
- /// <returns></returns>
- public bool StartWhiteReturn(Func<bool> action, int timeout = 3)
- {
- DateTime beginTime = DateTime.Now;
- bool doResult = false;
- while (DateTime.Now.Subtract(beginTime).TotalSeconds <= 3 && !doResult)
- {
- doResult = action();
- }
- return doResult;
- }
- /// <summary>
- ///
- /// </summary>
- public event ActionKeyHandle ActionAbort;
- /// <summary>
- ///
- /// </summary>
- public event ActionKeyHandle ActionStarted;
- }
- }
|