Guest User

Untitled

a guest
Apr 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. Parallel.ForEach<ListViewItem>(filesListView.Items.Cast<ListViewItem>(), new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }, item => {
  2. if (CallToStop == true)
  3. {
  4. //Code here to stop the loop!
  5. }
  6. internalProcessStart(item);
  7. });
  8.  
  9. Parallel.ForEach<ListViewItem>(filesListView.Items.Cast<ListViewItem>(),
  10. new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount },
  11. (item, state) => {
  12. if (CallToStop == true)
  13. {
  14. state.Break();
  15. }
  16. internalProcessStart(item);
  17. });
  18.  
  19. Parallel.ForEach<ListViewItem>(filesListView.Items.Cast<ListViewItem>(), new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }, (item , state) =>
  20. {
  21. if (/*Stop condition here*/)
  22. {
  23. state.Break();
  24. }
  25. internalProcessStart(item);
  26. });
  27.  
  28. Parallel.ForEach<int>(new List<int>(),
  29. new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount },
  30. (val, loopState) =>
  31. {
  32. if (val == 9) //enter your stopcondition here
  33. {
  34. loopState.Stop();
  35. return;
  36. }
  37. });
  38.  
  39. // Use ParallelOptions instance to store the CancellationToken
  40. ParallelOptions po = new ParallelOptions();
  41. po.CancellationToken = cts.Token;
  42. po.MaxDegreeOfParallelism = Environment.ProcessorCount;
  43.  
  44. try
  45. {
  46. Parallel.ForEach<ListViewItem>(filesListView.Items.Cast<ListViewItem>(), po, item => {
  47. // po.CancellationToken.ThrowIfCancellationRequested(); //1
  48. if (CallToStop == true)
  49. {
  50. //Code here to stop the loop!
  51. cts.Cancel();
  52. }
  53. if (po.CancellationToken.IsCancellationRequested == false)
  54. {
  55. internalProcessStart(item);
  56. }
  57. });
  58. }
  59. catch (OperationCanceledException e)
  60. {
  61. // handle
  62. }
  63. finally
  64. {
  65. cts.Dispose();
  66. }
Add Comment
Please, Sign In to add comment