Rakesh__
New member
- Joined
- Jan 25, 2023
- Messages
- 4
- Programming Experience
- 1-3
I am using Microsoft Visual Studio Community 2022 (64-bit) - Current Version 17.3.1
.Net version 6.0
The below code, hangs in the middle when run from release mode.
This code pings each ip address 4 times asynchronously, and the entire function is made to run on 5k IP addresses using task.Add and Task.WhenAll.
Works perfectly fine in debug mode, but stops in the middle when run on release mode.
I also tries to remove the for loop in the IcmpPing class to just ping once, but still won't run in the release mode.
If I run for smaller number of IP addresses, like 100-800. it runs with no issues, but when I increase the number to above 1000, it starts to run and completes 50-60% of the ip's and just hangs or stops/pauses after that,
without any errors
.Net version 6.0
The below code, hangs in the middle when run from release mode.
This code pings each ip address 4 times asynchronously, and the entire function is made to run on 5k IP addresses using task.Add and Task.WhenAll.
Works perfectly fine in debug mode, but stops in the middle when run on release mode.
I also tries to remove the for loop in the IcmpPing class to just ping once, but still won't run in the release mode.
If I run for smaller number of IP addresses, like 100-800. it runs with no issues, but when I increase the number to above 1000, it starts to run and completes 50-60% of the ip's and just hangs or stops/pauses after that,
without any errors
Code:
using Microsoft.VisualBasic;
using System.Net.NetworkInformation;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Threading.Tasks;
using System.Diagnostics;
namespace PingMinimal
{
internal class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Started!");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
string jsonFile = File.ReadAllText(@"C:\Users\inputFile.json");
// Json FIle contains a dictionary of length 10k keys, and has the format
// {"DE08W2534": ["10.218.131.72", true], "de08w2574": ["172.31.232.204", true],.......}
JObject jsonObj = JsonConvert.DeserializeObject<JObject>(jsonFile);
IEnumerable<JProperty> first5000Values = jsonObj.Children<JProperty>().Take(5000);
List<Task> tasks = new List<Task>();
foreach (JProperty pair in first5000Values)
{
tasks.Add(IcmpPing.PingIP(pair.Value[0].ToString()));
};
await Task.WhenAll(tasks);
stopwatch.Stop();
Console.WriteLine("Total Elapsed Time {0}", stopwatch.Elapsed);
Console.WriteLine("Completed!");
}
}
internal class IcmpPing
{
public static async Task PingIP(string ip)
{
using (Ping pingInstance = new Ping())
{
for (int i = 0; i < 4; i++)
{
try
{
PingReply reply = await pingInstance.SendPingAsync(ip);
}
catch
{
}
}
}
}
}
}