Exemplo n.º 1
0
        /// <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);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
 /// <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);
     }
 }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        /// <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);
            }
        }
Exemplo n.º 6
0
 /// <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;
     }
 }
Exemplo n.º 7
0
 public static void Queue(ThreadPoolName uniqueId, Action action)
 {
     Queue(uniqueId, action, null);
 }
Exemplo n.º 8
0
 public static void SetMaxThreads(ThreadPoolName uniqueId, int threads)
 {
     GetThreadPool(uniqueId).SetMaxThreads(threads);
 }
Exemplo n.º 9
0
 public static void Queue(ThreadPoolName uniqueId, Action action, Action done)
 {
     Queue(uniqueId, action, done, false);
 }
Exemplo n.º 10
0
 public static void Queue(ThreadPoolName uniqueId, Action action, Action done, bool urgent)
 {
     Queue(uniqueId, action, done, urgent, 0);
 }
Exemplo n.º 11
0
 public static void Queue(ThreadPoolName uniqueId, Action action)
 {
     Queue(uniqueId, action, null);
 }
Exemplo n.º 12
0
 public static void Queue(ThreadPoolName uniqueId, Action action, int delay)
 {
     Queue(uniqueId, action, null, false, delay);
 }
Exemplo n.º 13
0
 public static void Queue(ThreadPoolName uniqueId, Action action, int delay)
 {
     Queue(uniqueId, action, null, false, delay);
 }
Exemplo n.º 14
0
 public static void SetMaxThreads(ThreadPoolName uniqueId, int threads)
 {
     GetThreadPool(uniqueId).SetMaxThreads(threads);
 }
Exemplo n.º 15
0
 /// <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;
     }
 }
Exemplo n.º 16
0
 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;
 }
Exemplo n.º 17
0
        /// <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);
        }
Exemplo n.º 18
0
 public static void Queue(ThreadPoolName uniqueId, Action action, Action done, bool urgent)
 {
     Queue(uniqueId, action, done, urgent, 0);
 }
Exemplo n.º 19
0
 public static void Queue(ThreadPoolName uniqueId, Action action, Action done)
 {
     Queue(uniqueId, action, done, false);
 }