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); } }
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))); } }
/// <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); }