2

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!

Brain Bytes
  • 121
  • 1
  • 12
  • If you are talking about [this issue](https://stackoverflow.com/questions/17756824/blue-screen-when-using-ping), it only happens while you're debugging. Note that since the bug is in a low-level OS component (not in Visual Studio), using a different mechanism for pinging is unlikely to work-around the problem, since that will still go through the same component (i.e. this isn't a problem with the `Ping` class per se). See marked duplicate, you can use `Debugger.IsAttached` to disabling the pinging feature while the debugger is attached. – Peter Duniho Jul 31 '17 at 03:18
  • Using the `Ping` class the way you are and working around the debugging issue is still probably your best bet, but in the interest of providing alternatives WMI does have a [`Win32_PingStatus` class](https://msdn.microsoft.com/library/aa394350.aspx). – Lance U. Matthews Jul 31 '17 at 03:20
  • @PeterDuniho Yeah I've seen those, I was hoping my wishful thinking was about to come true and could find some obscure rusty pinging api that is not this one. Oh well, I ll try something I thing BACON gave me an idea. Thank you though – Brain Bytes Jul 31 '17 at 04:16
  • @BACON Thank you I think you gave me an idea to debug it, I will post the solution if it works – Brain Bytes Jul 31 '17 at 04:17
  • @PeterDuniho This question has not been answered, the answers out there do not fix this problem in programmatic way, I think I have an idea please could you remove the "marked as duplicate" and if I don't find a way around I ll delete it. Thx – Brain Bytes Jul 31 '17 at 04:18
  • 1
    @BrainBytes You could also P/Invoke the Win32 [`IcmpSendEcho` function](https://msdn.microsoft.com/library/windows/desktop/aa366050.aspx), which, in a very simplistic sense, is what the [`Ping` class](http://referencesource.microsoft.com/#System/net/System/Net/NetworkInformation/ping.cs,673) is doing, anyways. – Lance U. Matthews Jul 31 '17 at 04:24
  • @BrainBytes You can get a question closed as a duplicate reopened when you edit it with an explanation of why the linked question doesn't fix the problem. The question will enter the reopen queue when it is edited and will be reopened when the reviewers agree that it should be reopened. – Modus Tollens Jul 31 '17 at 04:24
  • 1
    _"if I don't find a way around I ll delete it"_ -- if you _do_ find a way around it, the answer belongs on the marked duplicate, which is also a request for a work-around and already has a significant amount of relevant information provided there. There are many different ways you could send a ping request from your PC, but all but the most ambitious alternatives will all through tcpip.sys. The fact that no one else has yet found a work-around does not change the duplicate nature of your question. – Peter Duniho Jul 31 '17 at 04:28
  • Please don't post the solution to your question in the question itself. If it fits, post it as an answer on the linked duplicate instead. Posting the answer inside the question prevents it from getting votes by itself. – Modus Tollens Aug 02 '17 at 17:05
  • @ModusTollens How do I answer? They closed the topic – Brain Bytes Aug 02 '17 at 18:54
  • @BrainBytes The linked duplicate is not closed. You can check if your answer fits that question and post it there. – Modus Tollens Aug 02 '17 at 22:43

0 Answers0