Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Parallel.ForEach<ListViewItem>(filesListView.Items.Cast<ListViewItem>(), new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }, item => {
- if (CallToStop == true)
- {
- //Code here to stop the loop!
- }
- internalProcessStart(item);
- });
- Parallel.ForEach<ListViewItem>(filesListView.Items.Cast<ListViewItem>(),
- new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount },
- (item, state) => {
- if (CallToStop == true)
- {
- state.Break();
- }
- internalProcessStart(item);
- });
- Parallel.ForEach<ListViewItem>(filesListView.Items.Cast<ListViewItem>(), new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }, (item , state) =>
- {
- if (/*Stop condition here*/)
- {
- state.Break();
- }
- internalProcessStart(item);
- });
- Parallel.ForEach<int>(new List<int>(),
- new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount },
- (val, loopState) =>
- {
- if (val == 9) //enter your stopcondition here
- {
- loopState.Stop();
- return;
- }
- });
- // Use ParallelOptions instance to store the CancellationToken
- ParallelOptions po = new ParallelOptions();
- po.CancellationToken = cts.Token;
- po.MaxDegreeOfParallelism = Environment.ProcessorCount;
- try
- {
- Parallel.ForEach<ListViewItem>(filesListView.Items.Cast<ListViewItem>(), po, item => {
- // po.CancellationToken.ThrowIfCancellationRequested(); //1
- if (CallToStop == true)
- {
- //Code here to stop the loop!
- cts.Cancel();
- }
- if (po.CancellationToken.IsCancellationRequested == false)
- {
- internalProcessStart(item);
- }
- });
- }
- catch (OperationCanceledException e)
- {
- // handle
- }
- finally
- {
- cts.Dispose();
- }
Add Comment
Please, Sign In to add comment