Ejemplo n.º 1
0
 public DelegateInstallationMiddleware(
     AppliesTo <InstallationContext> appliesTo,
     InvokeAsync <InstallationContext> invokeAsync)
 {
     _appliesTo   = appliesTo ?? throw new ArgumentNullException(nameof(appliesTo));
     _invokeAsync = invokeAsync ?? throw new ArgumentNullException(nameof(invokeAsync));
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Parallel ForEach method.
        /// </summary>
        /// <remarks>Source: http://www.codeproject.com/KB/dotnet/PoorMansParallelForEach.aspx</remarks>
        public static void ForEach <T>(IEnumerable <T> enumerable, Action <T> action)
        {
            int threadCount = Environment.ProcessorCount;

            object syncRoot = new object();

            if (enumerable == null)
            {
                return;
            }

            IEnumerator <T> enumerator = enumerable.GetEnumerator();

            InvokeAsync <T> del = InvokeAction;

            T[] seedItemArray = new T[threadCount];
            List <IAsyncResult> resultList = new List <IAsyncResult>(threadCount);

            for (int i = 0; i < threadCount; i++)
            {
                bool moveNext;

                lock (syncRoot)
                {
                    moveNext         = enumerator.MoveNext();
                    seedItemArray[i] = enumerator.Current;
                }

                if (moveNext)
                {
                    IAsyncResult iAsyncResult = del.BeginInvoke
                                                    (enumerator, action, seedItemArray[i], syncRoot, i, null, null);
                    resultList.Add(iAsyncResult);
                }
            }

            foreach (IAsyncResult iAsyncResult in resultList)
            {
                del.EndInvoke(iAsyncResult);
                iAsyncResult.AsyncWaitHandle.Close();
            }
        }
Ejemplo n.º 3
0
        public static void ForEach <T>(IEnumerable <T> enumerable, System.Action <T> action)
        {
            var syncRoot = new object();

            if (enumerable == null)
            {
                return;
            }

            var enumerator = enumerable.GetEnumerator();

            InvokeAsync <T> del = InvokeAction;

            var seedItemArray = new T[NumberOfParallelTasks];
            var resultList    = new List <System.IAsyncResult>(NumberOfParallelTasks);

            for (int i = 0; i < NumberOfParallelTasks; i++)
            {
                bool moveNext;

                lock (syncRoot)
                {
                    moveNext         = enumerator.MoveNext();
                    seedItemArray[i] = enumerator.Current;
                }

                if (moveNext)
                {
                    var iAsyncResult = del.BeginInvoke
                                           (enumerator, action, seedItemArray[i], syncRoot, i, null, null);
                    resultList.Add(iAsyncResult);
                }
            }

            for (int i = 0, resultListCount = resultList.Count; i < resultListCount; i++)
            {
                var iAsyncResult = resultList[i];
                del.EndInvoke(iAsyncResult);
                iAsyncResult.AsyncWaitHandle.Close();
            }
        }
Ejemplo n.º 4
0
 // Alias to match the name of delegate.Invoke
 public static ValueTask Invoke <T>(
     this InvokeAsync <T> invocation,
     T context,
     CancellationToken cancellationToken = default)
 => invocation.InvokeAsync(context, cancellationToken);