C#程序导致蓝屏?

时间:2011-11-02 04:23:34

标签: c# sockets thread-safety

这只是蓝屏显示的重要内容。我使用的是Windows 7 x64。

“检测到问题,Windows已关闭以防止损坏 到你的电脑。

PROCESS_HAS_LOCKED_PAGES

* STOP:0x00000076(0x0000000000000000,0xfffffa8009dcd060,0x0000000000000011, 0x0000000000000000)

我现在无法继续工作,因为每次关闭它都会得到一个蓝屏! 除了运行下面的后台工作程序之外,该程序还没有做任何事情。它ping所有可能属于用户家庭网络的地址,并尝试连接到另一个程序将监听的某个端口。

private void NetworkScanner_DoWork(object sender, DoWorkEventArgs e)
    {
        bool ExceptionEncountered = false;
        int IPsProcessed = 0;

        NetworkSearcherOutput = "Starting network scanner...";
        NetworkSearcher.ReportProgress(0);
        Thread.Sleep(1000);

        foreach (IPAddress IP in Dns.GetHostAddresses(Dns.GetHostName()))
        {
            if (IP.AddressFamily == AddressFamily.InterNetwork)
            {
                string[] Octets = IP.ToString().Split('.');
                Octets[3] = "0";

                IPAddress CurrentAddressIteration = StringArrayToIP(Octets);
                while (GetLastOctet(CurrentAddressIteration) != 255)
                {
                    PingReply Reply = new Ping().Send(CurrentAddressIteration, 5);

                    if (Reply.Status == IPStatus.Success)
                    {
                        NetworkSearcherOutput = CurrentAddressIteration.ToString() + " sent response.";
                        NetworkSearcher.ReportProgress(0);
                        Thread.Sleep(500);

                        InClient Client = new InClient(CurrentAddressIteration);

                        try
                        {
                            Client.Connect();

                            SnapshotBox.Image = Client.Receive(typeof(Image));

                            NetworkSearcherOutput = CurrentAddressIteration.ToString() + " is running program.";
                            NetworkSearcher.ReportProgress(0);
                            Thread.Sleep(1000);
                        }

                        catch (Exception E)
                        {
                            // A socket exception is expected when the client is not running the program.
                            if (E is SocketException)
                            {
                                Client.Close();

                                NetworkSearcherOutput = CurrentAddressIteration.ToString() + " is not running program.";
                                NetworkSearcher.ReportProgress(0);
                                Thread.Sleep(1000);
                            }

                            //Unhandled exception. Show messagebox and close.
                            else
                            {
                                MessageBox.Show("Network scanner encountered an unhandled exception.\n\n" + E.GetType().ToString() + ": " + E.Message, "Unhandled Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                ExceptionEncountered = true;
                                break;
                            }
                        }
                    }

                    else
                    {
                        NetworkSearcherOutput = CurrentAddressIteration.ToString() + " did not respond.";
                        NetworkSearcher.ReportProgress(0);
                    }

                    IPsProcessed++;

                    if (IPsProcessed == 5)
                    {
                        NetworkSearcher.ReportProgress(2);
                        IPsProcessed = 0;
                    }

                    Octets = CurrentAddressIteration.ToString().Split('.');
                    Octets[3] = (Int32.Parse(Octets[3]) + 1).ToString();
                    CurrentAddressIteration = StringArrayToIP(Octets);
                }
            }
        }

        if (!ExceptionEncountered)
        {
            NetworkSearcherOutput = "Network scanning complete.";
            NetworkSearcher.ReportProgress(0);
            NetworkSearcher.ReportProgress(100);
        }

        else
        {
            NetworkSearcherOutput = "Network scanning encountered an error.";
            NetworkSearcher.ReportProgress(-1);
        }

我认为C#程序应该永远不会导致蓝屏?

2 个答案:

答案 0 :(得分:7)

几个星期前我发现了这个问题。它只在使用.NET 4时才会发生。

报告MS Connect

修改

(Will *)将此链接添加到MS Connect错误报告。

* login.live.com再次进入无限循环......

答案 1 :(得分:4)

为了清楚起见,“用户”模式代码无法在Windows中强制创建蓝屏,除非它使用未记录的API和/或强制将坏数据导入驱动程序。你的C#代码可能没有错,就好像你使用用户模式类(套接字)那样套接字负责不破坏你的计算机。

由于@Joe已评论Microsoft Support KB Article 256010清楚地描述了此停止消息,但更好的是有关于捕获负责此错误的驱动程序名称的明确说明。

请注意,您安装的任何软件防火墙也涉及内核模式级别,因此也可能导致此错误。我建议您按照知识库文章的建议进行操作,并尝试找出问题所在。但您也可以确保已将网络驱动程序和防火墙/ VPN软件更新到最新的稳定版本。