How to do it...

  1. In the Demo class, add a method called CheckClientMachinesOnline(), which takes as parameters a List<string> collection of IP addresses and an integer that specifies the minimum number of machines required to be online. Add a second method called MachineReturnedPing(), which will receive an IP address to ping. For our purpose, we will just return false to mimic a dead machine (the ping to the IP address timed out):
        public class Recipes 
{
public void CheckClientMachinesOnline(List<string> ipAddresses,
int minimumLive)
{

}

private bool MachineReturnedPing(string ip)
{
return false;
}
}
  1. Inside the CheckClientMachinesOnline() method, add the Parallel.ForEach loop and create the ParallelOptions variable that will specify the degree of parallelism. Wrap all this code inside a try...catch statement and catch an AggregateException:
        try 
{
int machineCount = ipAddresses.Count();
var options = new ParallelOptions();
options.MaxDegreeOfParallelism = machineCount;
int deadMachines = 0;

Parallel.ForEach(ipAddresses, options, ip =>
{

});
}
catch (AggregateException aex)
{
WriteLine("An AggregateException has occurred");
throw;
}
  1. Inside the Parallel.ForEach loop, write the code to check whether the machine is online by calling the MachineReturnedPing() method. In our example, this method will always return false. You will notice that we are keeping track of the offline machine count via the Interlocked.Increment method. This is just a way of incrementing a variable across the threads of the Parallel.ForEach loop:
        if (MachineReturnedPing(ip)) 
{

}
else
{
if (machineCount - Interlocked.Increment(ref deadMachines)
< minimumLive)
{
WriteLine($"Machines to check = {machineCount}");
WriteLine($"Dead machines = {deadMachines}");
WriteLine($"Minimum machines required = {minimumLive}");
WriteLine($"Live Machines = {machineCount - deadMachines}");
throw new Exception($"Minimum machines requirement of
{minimumLive} not met");
}
}
  1. If you have added all the code correctly, your Demo class will look like this:
        public class Demo 
{
public void CheckClientMachinesOnline(List<string> ipAddresses,
int minimumLive)
{
try
{
int machineCount = ipAddresses.Count();
var options = new ParallelOptions();
options.MaxDegreeOfParallelism = machineCount;
int deadMachines = 0;

Parallel.ForEach(ipAddresses, options, ip =>
{
if (MachineReturnedPing(ip))
{

}
else
{
if (machineCount - Interlocked.Increment(
ref deadMachines) < minimumLive)
{
WriteLine($"Machines to check = {machineCount}");
WriteLine($"Dead machines = {deadMachines}");
WriteLine($"Minimum machines required =
{minimumLive}");
WriteLine($"Live Machines = {machineCount -
deadMachines}");
throw new Exception($"Minimum machines requirement
of {minimumLive} not met");
}
}
});
}
catch (AggregateException aex)
{
WriteLine("An AggregateException has occurred");
throw;
}
}

private bool MachineReturnedPing(string ip)
{
return false;
}
}
  1. In the console application, create the List<string> object to store a collection of dummy IP addresses. Instantiate your Demo class and call the CheckClientMachinesOnline() method, passing the collection of IP addresses and the minimum number of machines required to be online to it:
        List<string> ipList = new List<string>(); 
for (int i = 0; i <= 10; i++)
{
ipList.Add($"10.0.0.{i.ToString()}");
}

try
{
Demo oRecipe = new Demo();
oRecipe.CheckClientMachinesOnline(ipList, 2);
}
catch (Exception ex)
{
WriteLine(ex.InnerException.Message);
}
ReadLine();
  1. Run your application and review the output in the console window:
Just a point to note. If you have Just My Code enabled, in some cases Visual Studio will break on the line that throws the exception. It might also say that the exception is not handled by the user code. You can just press F5 to continue. To prevent this from happening, uncheck Enable Just My Code under Tools, Options, Debugging, and General.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.212.71