using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Parallel_For_Stop
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\n\nExample Number 1\n");
Parallel.For(1, 201, (i, loopState) => //you do not need to call loopstate you can call it whatever you want
{
int j = i;
if (i > 100) //it will stop when reaches 101 .....if we put i == 100 it will stop in 100..only stop the cycles that are above the breaking cycle
//READ THIS: https://msdn.microsoft.com/en-us/library/dd460721(v=vs.110).aspx
{
//when running, the cycles are random....there is not first cycle or last cycle
Console.WriteLine("Stop on index: {0}", j);
//loopState.Break(); //we changed...the break to stop ... class 2/27/2015
loopState.Stop();
}
Console.WriteLine("index:{0} taskid: {1}", j.ToString().PadRight(9), Task.CurrentId);
});
//Example
//Use a Parallel.For that adds all the ints between 1000 and 1200 stop when the sum exceeds 5000
//Display Cycle number and sum
Console.WriteLine("\n\nExample Number 2\n");
long sum = 0;
Parallel.For(1, 200, (i, loopState) =>
{
int j = i;
sum += j + 1000;
if (sum > 5000)//it will breake when reach 5000
{
Console.WriteLine("Stop Cycle: {0}", j);
//loopState.Break(); we did this in class 2/27/2015
loopState.Stop();
}
else
{
Console.WriteLine("Cycle:{0} sum : {1}", j, sum);
}
});
Console.ReadLine();
}
}
}
///lab assigment Due Monday
///Console Application with a menu to run a parallel loop out of 5 parallel loops
///int the menu, each loop to run the SumRoot we did before
///In the menu
///1. Parallel.For provide break statement
///2. Parallel.Foreach provide break statement
///3. Paralell.For provide break statment
///4. Paralell.Foreach provide break statment
///5. Paralell.Foreach provide cancellation
///
////Provide at least 12 text files (like .cs or .txt)(save them to the Debug folder)
////write a parallel for, where each cycle is to open one of the text file and count
//the number of vowels in the file, then display the filename and the final count of a single file
No comments:
Post a Comment