Parallel Execution - ZjzMisaka/PowerThreadPool GitHub Wiki
Allows for the efficient execution of loops in parallel using multiple threads. This functionality supports indexed loops (For
), collection-based loops (ForEach
), and monitoring of the ConcurrentObservableCollection
(Watch
).
For
designed to execute a loop in parallel, iterating over a specified range of indices. It can handle different types of collections and provides flexibility with various overloads.
-
For(int start, int end, Action<int> body, int step = 1, string groupName = null)
Executes a loop fromstart
toend
with the specified action to be performed on each index.// Do something before loop powerPool.For(0, 10, (i) => DoSomething(i)).Wait(); // Do something after loop
-
For<TSource>(int start, int end, IList<TSource> source, Action<TSource> body, int step = 1, string groupName = null)
Executes a loop over a collection fromstart
toend
, performing the specified action on each element.// Do something before loop powerPool.For(0, 10, list, (item) => DoSomething(item)).Wait(); // Do something after loop
-
For<TSource>(int start, int end, IList<TSource> source, Action<TSource, int> body, int step = 1, string groupName = null)
Executes a loop over a collection fromstart
toend
, performing the specified action on each element and its index.// Do something before loop powerPool.For(0, 10, list, (item, i) => DoSomething(item, i)).Wait(); // Do something after loop
-
start
: The start index of the loop. -
end
: The end index of the loop. -
source
: The source collection of elements to be processed in the loop. -
body
: The action to execute for each loop iteration, receiving an element from the source collection and the iteration index. -
step
(optional): The step value for each loop iteration. Default is 1. -
groupName
(optional): The optional name for the group. Default is null.
ForEach
is designed to iterate over a collection in parallel, applying a specified action to each element. It is suitable for scenarios where the loop needs to process elements of a collection concurrently.
-
ForEach<TSource>(IEnumerable<TSource> source, Action<TSource> body, string groupName = null)
Executes a loop over a collection, performing the specified action on each element.// Do something before loop powerPool.ForEach(list, (item) => DoSomething(item)).Wait(); // Do something after loop
-
ForEach<TSource>(IEnumerable<TSource> source, Action<TSource, int> body, string groupName = null)
Executes a loop over a collection, performing the specified action on each element and its index.// Do something before loop powerPool.ForEach(list, (item, i) => DoSomething(item, i)).Wait(); // Do something after loop
-
source
: The source collection of elements to be processed. -
body
: The action to execute for each element in the source collection and its index. -
groupName
(optional): The optional name for the group. Default is null.
Watch
monitors an observable collection for changes and processes each element using a specified action. This method is particularly useful when you need to handle dynamic collections and perform operations on their elements as changes occur.
When the collection changes, all elements within the collection will be extracted and passed to the user's action for processing. If the work is canceled, stopped, or an exception occurs, the elements will be added back to the collection.
-
Watch<TSource>(ConcurrentObservableCollection<TSource> source, Action<TSource> body, bool addBackWhenWorkCanceled = true, bool addBackWhenWorkStopped = true, bool addBackWhenWorkFailed = true, string groupName = null)
Watches an observable collection for changes and processes each element in the collection using the specified action.// Do something before watch powerPool.Watch(collection, (i) => Dosomething(i)); // Do something after watch
-
source
:
The observable collection (ConcurrentObservableCollection<TSource>
) of elements to be monitored and processed. -
body
:
The action (Action<TSource>
) to execute for each element in the collection. This function defines the logic to be applied to individual items. -
addBackWhenWorkCanceled
(optional):
If the work is canceled, the elements will be added back to the collection. Default istrue
. -
addBackWhenWorkStopped
(optional):
If the work is stopped, the elements will be added back to the collection. Default istrue
. -
addBackWhenWorkFailed
(optional):
If an exception occurs, the elements will be added back to the collection. Default istrue
. -
groupName
(optional):
A string representing the name of the group. If not provided, a unique identifier will be generated automatically. Default isnull
.
StopWatching
stops monitoring an observable collection for changes. This method is useful when you no longer need to process updates to a collection or when the associated tasks should be halted.
-
StopWatching<TSource>(ConcurrentObservableCollection<TSource> source, bool keepRunning = false, bool forceStop = false)
Stops watching the observable collection for changes.// After watching powerPool.StopWatching(collection);
-
source
:
The observable collection (ConcurrentObservableCollection<TSource>
) whose monitoring should be stopped. -
keepRunning
(optional):
A boolean indicating whether to allow existing tasks to continue running. Default isfalse
. -
forceStop
(optional):
A boolean indicating whether to forcibly stop all ongoing tasks. Default isfalse
.