/// <summary> /// /// </summary> /// <param name="uniqueId"></param> /// <param name="action"></param> /// <param name="done"></param> /// <param name="urgent"></param> /// <param name="delay">Millisecond delay before executing </param> public static void Queue(ThreadPoolName uniqueId, Action action, Action done, bool urgent, int delay) { //Debug.Assert(uniqueId != null); Debug.Assert(action != null); Action workItem = () => { //Logger.ReportVerbose("Starting work item"); try { action(); } catch (ThreadAbortException) { /* dont report on this, its normal */ } catch (Exception ex) { Logger.ReportException("Async thread threw the following exception: ", ex); Debug.Assert(false, "Async thread threw the following exception: " + ex.ToString()); } //Logger.ReportVerbose("Finished work item"); if (done != null) { done(); } //Logger.ReportVerbose("Finished done callback for work item"); }; //Logger.ReportVerbose("Getting threadpool " + uniqueId); GetThreadPool(uniqueId).Queue(workItem, urgent, delay); }
private static ThreadPoolName GetThreadPoolNameFromLegacyString(string uniqueId) { Logger.ReportVerbose("=========== Action called on legacy thread name: {0}", uniqueId); ThreadPoolName name = ThreadPoolName.Legacy; try { name = (ThreadPoolName)Enum.Parse(typeof(ThreadPoolName), uniqueId); } catch { } return(name); }
/// <summary> /// Makes a call on a different thread while holding the current thread until it completes or a timeout occurs. /// If an exception is thrown on the other thread it will be re-thrown on this thread /// If a timeout occurs the call will still continue to execute until it actually times out. /// </summary> /// <param name="uniqueId"></param> /// <param name="action"></param> /// <param name="timeout"></param> /// <returns></returns> public static bool CallWithTimeout(ThreadPoolName uniqueId, Action action, TimeSpan timeout) { using (AutoResetEvent ev = new AutoResetEvent(false)) { Exception exception = null; Queue(uniqueId, () => { try { action(); } catch (Exception ex) { exception = ex; } finally { ev.Set(); } }); bool ret = ev.WaitOne(timeout); if (exception != null) { throw exception; } return(ret); } }
private static ThreadPool GetThreadPool(ThreadPoolName uniqueId) { uniqueId = TranslatePool(uniqueId); ThreadPool currentPool; lock (threadPool) { if (!threadPool.TryGetValue(uniqueId, out currentPool)) { currentPool = new ThreadPool(uniqueId.ToString()); threadPool[uniqueId] = currentPool; currentPool.SetMaxThreads(10); } } return(currentPool); }
/// <summary> /// Determines which pool a request thread identifier will actually run in. /// </summary> /// <param name="name"></param> /// <returns></returns> private static ThreadPoolName TranslatePool(ThreadPoolName name) { //Logger.ReportVerbose("================= Action called on threadpool: {0}", name.ToString()); switch (name) { case ThreadPoolName.WaitForExternalPlayerToLaunch: case ThreadPoolName.WaitForProcessToExit: case ThreadPoolName.SqliteDisplayWriter: case ThreadPoolName.SqliteWriter: case ThreadPoolName.FastLoadLoader: case ThreadPoolName.TVRefresh: case ThreadPoolName.RefreshUI: case ThreadPoolName.UnwatchedCounterForFolderModel: case ThreadPoolName.Legacy: return(name); default: return(ThreadPoolName.Common); } }
/// <summary> /// Determines which pool a request thread identifier will actually run in. /// </summary> /// <param name="name"></param> /// <returns></returns> private static ThreadPoolName TranslatePool(ThreadPoolName name) { //Logger.ReportVerbose("================= Action called on threadpool: {0}", name.ToString()); switch (name) { case ThreadPoolName.WaitForExternalPlayerToLaunch: case ThreadPoolName.WaitForProcessToExit: case ThreadPoolName.SqliteDisplayWriter: case ThreadPoolName.SqliteWriter: case ThreadPoolName.FastLoadLoader: case ThreadPoolName.TVRefresh: case ThreadPoolName.RefreshUI: case ThreadPoolName.UnwatchedCounterForFolderModel: case ThreadPoolName.Legacy: return name; default: return ThreadPoolName.Common; } }
public static void Queue(ThreadPoolName uniqueId, Action action) { Queue(uniqueId, action, null); }
public static void SetMaxThreads(ThreadPoolName uniqueId, int threads) { GetThreadPool(uniqueId).SetMaxThreads(threads); }
public static void Queue(ThreadPoolName uniqueId, Action action, Action done) { Queue(uniqueId, action, done, false); }
public static void Queue(ThreadPoolName uniqueId, Action action, Action done, bool urgent) { Queue(uniqueId, action, done, urgent, 0); }
public static void Queue(ThreadPoolName uniqueId, Action action, int delay) { Queue(uniqueId, action, null, false, delay); }
/// <summary> /// Makes a call on a different thread while holding the current thread until it completes or a timeout occurs. /// If an exception is thrown on the other thread it will be re-thrown on this thread /// If a timeout occurs the call will still continue to execute until it actually times out. /// </summary> /// <param name="uniqueId"></param> /// <param name="action"></param> /// <param name="timeout"></param> /// <returns></returns> public static bool CallWithTimeout(ThreadPoolName uniqueId, Action action, TimeSpan timeout) { using (AutoResetEvent ev = new AutoResetEvent(false)) { Exception exception = null; Queue(uniqueId, () => { try { action(); } catch (Exception ex) { exception = ex; } finally { ev.Set(); } }); bool ret = ev.WaitOne(timeout); if (exception != null) throw exception; return ret; } }
private static ThreadPool GetThreadPool(ThreadPoolName uniqueId) { uniqueId = TranslatePool(uniqueId); ThreadPool currentPool; lock (threadPool) { if (!threadPool.TryGetValue(uniqueId, out currentPool)) { currentPool = new ThreadPool(uniqueId.ToString()); threadPool[uniqueId] = currentPool; currentPool.SetMaxThreads(10); } } return currentPool; }
/// <summary> /// /// </summary> /// <param name="uniqueId"></param> /// <param name="action"></param> /// <param name="done"></param> /// <param name="urgent"></param> /// <param name="delay">Millisecond delay before executing </param> public static void Queue(ThreadPoolName uniqueId, Action action, Action done, bool urgent, int delay) { //Debug.Assert(uniqueId != null); Debug.Assert(action != null); Action workItem = () => { //Logger.ReportVerbose("Starting work item"); try { action(); } catch (ThreadAbortException) { /* dont report on this, its normal */ } catch (Exception ex) { Logger.ReportException("Async thread threw the following exception: ", ex); Debug.Assert(false, "Async thread threw the following exception: " + ex.ToString()); } //Logger.ReportVerbose("Finished work item"); if (done != null) done(); //Logger.ReportVerbose("Finished done callback for work item"); }; //Logger.ReportVerbose("Getting threadpool " + uniqueId); GetThreadPool(uniqueId).Queue(workItem, urgent, delay); }