示例#1
0
 public static unsafe void ForEach <T>(this T[] source, UnsafeAction <T> action, int step = 1) where T : unmanaged
 {
     fixed(T *sourcePtr = source)
     for (int i = 0; i < source.Length; i += step)
     {
         action(sourcePtr + i);
     }
 }
示例#2
0
        public static unsafe void ParallelForEach <T>(this T[] source, UnsafeAction <T> action, int step = 1) where T : unmanaged
        {
            fixed(T *sourcePtr = source)
            {
                T *sourcePtrProxy = sourcePtr;

                Parallel.For(0, source.Length / step, i => action(sourcePtrProxy + (i * step)));
            }
        }
示例#3
0
        /// <summary>
        /// Run's a unsafe action
        /// </summary>
        /// <param name="method">Unsafe method</param>
        public T RunSafe <T>(UnsafeAction <T> method)
        {
            T safeResult = default(T);

            try
            {
                safeResult = method.Invoke();
            }
            catch (Exception)
            {
            }

            return(safeResult);
        }