I am having a problem using Ping, in particular Ping.Send. This creates BSOD when I run it, and it's a known issue in visual studio.
Is there any way around this? I am just trying to see how fast a server responds, and no downloading something and timing it is not an option. Or is there a much more efficient way of doing this in the code itself?
Here is the method that causes BSOD, yes I have checked the crash dump and removed the method to check whether is this or not to cause it, I am 100% positive. I also tried to dispose of ping but not luck.
private string RoundTripCheck(string adress){
Ping ping = new Ping();
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 10000;
PingOptions options = new PingOptions(64, true);
PingReply reply = ping.Send(adress, timeout, buffer, options);
ping.Dispose();
if (reply.Status == IPStatus.Success){
return reply.RoundtripTime.ToString() + " ms";
}
return "Unavailable";
}
EDIT:
SOLUTION FOUND:
Step 1: Start a process with filename ping.exe Step 2: Enjoy!
private void StartDynamic(Control ctrl, ServerType svType){
Dictionary<string, string> tempDictionary = FindIt(ctrl, svType);
string[,] allProxies = new string[tempDictionary.Count, 4];
StringBuilder proxy = new StringBuilder();
string roundTripTest = "";
string location;
int count = 0; //Count is mainly there in case you don't get anything
Process process = new Process{
StartInfo = new ProcessStartInfo{
FileName = "ping.exe",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true,
}
};
for (int i = 0; i < tempDictionary.Count; i++){
proxy.Append(tempDictionary.Keys.ElementAt(i));
process.StartInfo.Arguments = proxy.ToString();
do{
try{
roundTripTest = RoundTripCheck(process);
}
catch (Exception ex){
count++;
}
if (roundTripTest == null){
count++;
}
if (count == 10 || roundTripTest.Trim().Equals("")){
roundTripTest = "Server Unavailable";
}
} while (roundTripTest == null);
}
process.Dispose();
}
RoundTripCheck method:
private string RoundTripCheck(Process p){
StringBuilder result = new StringBuilder();
string returned = "";
p.Start();
while (!p.StandardOutput.EndOfStream){
result.Append(p.StandardOutput.ReadLine());
if (result.ToString().Contains("Average")){
returned = result.ToString().Substring(result.ToString().IndexOf("Average ="))
.Replace("Average =", "").Trim().Replace("ms", "").ToString();
break;
}
result.Clear();
}
return returned;
}
BAM! Works like a charm!