Blue screen when using Ping

你离开我真会死。 提交于 2019-11-26 06:47:15

问题


I\'m running into the bug where it BSODon ending debugging in the middle of a ping.

I have a few ways to disable it in my (wpf) application (where I ping continuously), but sometimes I forget to do so and BSOD.

I\'d like to get around that say by changing a global AllowRealPinging variable and sleeping for 2 seconds in a callback before exiting the debugger so I don\'t BSOD.


回答1:


This is a known bug in Windows 7, you'll get a BSOD with bug-check code 0x76, PROCESS_HAS_LOCKED_PAGES in tcpip.sys when you terminate the process. The most relevant feedback article is here. Also covered in this SO question. No great answers there, the only known workaround is to fallback to a .NET version earlier than 4.0, it uses another winapi function that doesn't trigger the driver bug.

Avoiding pinging while you debug is certainly the best way to avoid this problem. Your desired approach is not going to work, your program is entirely frozen when it hits a breakpoint, kaboom when you stop debugging.

The simplest way is to just not starting pinging in the first place in the specific case of having a debugger attached. Use the System.Diagnostic.Debugger.IsAttached property to detect this in your code.




回答2:


This is a good way around:

private void GetPing(){

            Dictionary<string, string> tempDictionary = this.tempDictionary;  //Some adresses you want to test
            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 || roundTripTest.Equals(" ") || roundTripTest.Equals(""));
            }

            process.Dispose();

        }

RoundTripCheck method, where the magic happens:

       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;

        }

I had the same problem, this solves it!



来源:https://stackoverflow.com/questions/17756824/blue-screen-when-using-ping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!