/// <summary> /// Runs all the supplied functions concurrently, limited to the specified Max Threads. /// Blocks the executing thread until finished, then returns a /// collection of Try results, one for each request. /// </summary> /// <param name="funcs">The functions to run concurrently.</param> /// <param name="settings">Settings if required.</param> /// <typeparam name="TResultType">The functions result type.</typeparam> /// <returns>Returns a collection of results.</returns> public Try <TResultType>[] Run <TResultType>( IEnumerable <Func <TResultType> > funcs, ConcurrentFunctionSettings <TResultType> settings = null) { var tryFuncs = funcs.Select(f => fun(() => Try(() => f()))); return(Run(tryFuncs)); }
/// <summary> /// Runs all the supplied functions concurrently, limited to the specified Max Threads. /// Blocks the executing thread until finished, then returns a /// collection of Try results, one for each request. /// </summary> /// <param name="funcs">The functions to run concurrently.</param> /// <param name="settings">Settings if required.</param> /// <typeparam name="TResultType">The functions result type.</typeparam> /// <returns>Returns a collection of results.</returns> public Try <TResultType>[] Run <TResultType>( IEnumerable <Func <Try <TResultType> > > funcs, ConcurrentFunctionSettings <TResultType> settings = null) { var tasks = Execute(funcs, false, settings); return(ProcessAllTasks(tasks).ToArray()); }
/// <summary> /// Runs all the supplied functions concurrently limited to the specified Max Threads. /// Blocks the executing thread until finished. /// Will run until a task fails with an exception, and will return early, not running remaining tasks. /// </summary> /// <param name="funcs">The functions to run concurrently.</param> /// <param name="settings">Settings if required.</param> /// <typeparam name="TResultType">The functions result type.</typeparam> /// <returns>Returns a collection of results if all succeed, else first failing exception.</returns> public Try <TResultType[]> RunUntilError <TResultType>( IEnumerable <Func <Try <TResultType> > > funcs, ConcurrentFunctionSettings <TResultType> settings = null) { var tasks = Execute(funcs, true, settings); return(ProcessTasksAllOrFail(tasks).Map(_ => _.ToArray())); }
private Seq <FunctionResult <TResultType> > Execute <TResultType>( IEnumerable <Func <Try <TResultType> > > funcs, bool failOnFirstError, ConcurrentFunctionSettings <TResultType> settings = null) { var functionRequests = funcs.Select((func, idx) => new FunctionRequest <TResultType>(func, idx)).ToArray(); var tokenManager = new CancellationTokenManager(settings?.CancellationToken, failOnFirstError); var progressManager = new ProgressManager <TResultType>(settings?.Progress, functionRequests.Length); var results = functionRequests .Select(func => ExecuteFuncWhenReady(func, tokenManager, progressManager)) .ToArray(); // Ensure that we wait until all the running tasks are complete so the // Semaphore Slim doesn't get disposed before it's finished running and we exit. Task.WaitAll(results.ToArray <Task>()); return(results.Map(c => c.Result).ToSeq()); }
/// <summary> /// Runs all the supplied functions concurrently, limited to the specified Max Threads. /// Returns a RunnerResult object which can be used to wait for all tasks /// to be finished before checking the results. /// Runs all, any errors returned in the Try Result. /// </summary> /// <param name="funcs">The functions to run concurrently.</param> /// <param name="settings">Settings if required.</param> /// <typeparam name="TResultType">The functions result type.</typeparam> /// <returns>Returns a collection of results.</returns> public RunnerResult <Try <TResultType>[]> BeginRun <TResultType>( IEnumerable <Func <Try <TResultType> > > funcs, ConcurrentFunctionSettings <TResultType> settings = null) => new RunnerResult <Try <TResultType>[]>(Task.Run(() => Run(funcs, settings)));
/// <summary> /// Runs all the supplied functions concurrently, limited to the specified Max Threads. /// </summary> /// <param name="funcs">The functions to run concurrently.</param> /// <param name="maxThreads">The Max Threads to run.</param> /// <param name="settings">Settings if required.</param> /// <typeparam name="TResultType">The functions result type.</typeparam> /// <returns>Returns a collection of results.</returns> public static RunnerResult <Try <TResultType>[]> BeginRunConcurrently <TResultType>( this IEnumerable <Func <Try <TResultType> > > funcs, int maxThreads, ConcurrentFunctionSettings <TResultType> settings = null) => new ConcurrentFunctionRunner(maxThreads).BeginRun(funcs);
/// <summary> /// Runs all the supplied functions concurrently, limited to the specified Max Threads. /// </summary> /// <param name="funcs">The functions to run concurrently.</param> /// <param name="maxThreads">The Max Threads to run.</param> /// <param name="settings">Settings if required.</param> /// <typeparam name="TResultType">The functions result type.</typeparam> /// <returns>Returns a collection of results.</returns> public static Try <TResultType[]> RunConcurrentlyUntilError <TResultType>( this IEnumerable <Func <Try <TResultType> > > funcs, int maxThreads, ConcurrentFunctionSettings <TResultType> settings = null) => new ConcurrentFunctionRunner(maxThreads).RunUntilError(funcs);