Beispiel #1
0
 /// <summary>
 /// Iterates over elements in <paramref name="tsList"/> and calls <paramref name="handler"/>
 /// </summary>
 /// <typeparam name="T">Type to store in list</typeparam>
 /// <param name="tsList">A thread-safe list</param>
 /// <param name="handler">Called for each element</param>
 /// <param name="startIndex">Start index</param>
 /// <param name="endIndex">End index. <c>-1</c> means <see cref="ThreadSafe.IList{T}.Count_NoLock"/></param>
 /// <param name="reverseOrder"><c>true</c> if we should iterate in the reverse order</param>
 public static void Iterate <T>(this ThreadSafe.IList <T> tsList, int startIndex, int endIndex, bool reverseOrder, IterateDelegate <T> handler)
 {
     tsList.ExecuteLocked <object, object>(null, (tsList2, arg) => {
         if (reverseOrder)
         {
             int i = (endIndex < 0 ? tsList2.Count_NoLock : endIndex) - 1;
             for (; i >= startIndex; i--)
             {
                 if (!handler(tsList2, i, tsList2.Get_NoLock(i)))
                 {
                     break;
                 }
             }
         }
         else
         {
             // Count property can change so check it each time in the loop
             for (int i = startIndex; i < (endIndex < 0 ? tsList2.Count_NoLock : endIndex); i++)
             {
                 if (!handler(tsList2, i, tsList2.Get_NoLock(i)))
                 {
                     break;
                 }
             }
         }
         return(null);
     });
 }
Beispiel #2
0
        /// <summary>
        /// Locks the list and then calls <paramref name="handler"/>. <paramref name="handler"/>
        /// must only call <c>*_NoLock()</c> methods. The list is unlocked once this method returns.
        /// </summary>
        /// <typeparam name="T">List type</typeparam>
        /// <typeparam name="TArgType">Argument type</typeparam>
        /// <typeparam name="TRetType">Return type</typeparam>
        /// <param name="tsList">A list</param>
        /// <param name="arg">Passed to <paramref name="handler"/></param>
        /// <param name="handler">Handler that should execute when the lock is held</param>
        /// <returns>The value <paramref name="handler"/> returns</returns>
        public static TRetType ExecuteLocked <T, TArgType, TRetType>(this ThreadSafe.IList <T> tsList, TArgType arg, ExecuteLockedDelegate <T, TArgType, TRetType> handler)
        {
#if THREAD_SAFE
            return(tsList.ExecuteLocked <TArgType, TRetType>(arg, handler));
#else
            return(handler(tsList, arg));
#endif
        }
 /// <summary>
 /// Writes an element to the list
 /// </summary>
 /// <typeparam name="T">Type to store in list</typeparam>
 /// <param name="tsList">A thread-safe list</param>
 /// <param name="index">Index</param>
 /// <param name="value">Value</param>
 /// <returns><c>true</c> if <paramref name="value"/> was written to the list or <c>false</c>
 /// if <paramref name="index"/> was invalid.</returns>
 public static bool Set <T>(this ThreadSafe.IList <T> tsList, int index, T value)
 {
     return(tsList.ExecuteLocked <object, bool>(null, (tsList2, arg) => {
         if ((uint)index < (uint)tsList2.Count_NoLock)
         {
             tsList2.Set_NoLock(index, value);
             return true;
         }
         else
         {
             return false;
         }
     }));
 }
        /// <summary>
        /// Reads an element from the list
        /// </summary>
        /// <typeparam name="T">Type to store in list</typeparam>
        /// <param name="tsList">A thread-safe list</param>
        /// <param name="index">Index</param>
        /// <param name="value">Updated with value</param>
        /// <returns><c>true</c> if <paramref name="value"/> was updated with the element in the
        /// list or <c>false</c> if <paramref name="index"/> was invalid.</returns>
        public static bool Get <T>(this ThreadSafe.IList <T> tsList, int index, out T value)
        {
            bool retValue = false;

            value = tsList.ExecuteLocked <object, T>(null, (tsList2, arg) => {
                if ((uint)index < (uint)tsList2.Count_NoLock)
                {
                    retValue = true;
                    return(tsList2.Get_NoLock(index));
                }
                else
                {
                    retValue = false;
                    return(default(T));
                }
            });
            return(retValue);
        }