简体   繁体   English

WPF C#应用程序将冻结我的整个计算机,每次运行2-3次

[英]WPF C# application will freeze my whole computer ever 2-3 times I run it

I put a lot of information in this issue because I have no idea what will be relavent 我在这个问题上投入了很多信息,因为我不知道会发生什么

Issue: 问题:
I am having an issue with a program I am working on where when running it, it will freeze my whole computer and return no error (I am completely incapable of doing anything CTRL+ALT+DEL doesn't even work). 我在一个程序上遇到问题,该程序在哪里运行时,它将冻结我的整个计算机,并且不返回任何错误(我完全无法执行任何CTRL + ALT + DEL都不起作用的操作)。 This program accepts a connection from a android client and atm the android client is not configured correctly so the connection is being rejected. 此程序接受来自android客户端的连接,并且atm android客户端的配置不正确,因此该连接被拒绝。

Question: 题:
How can I stop my program from freezing my entire machine? 如何停止我的程序冻结整个机器?

Conjecture: 推测:
I have a few theories as to what is going on but no idea how to fix them. 我对发生的事情有一些理论,但不知道如何解决。 I have read that this may have something to do with me running a single threaded process inside my async worker but I am not sure that the socket is a single threaded process. 我已经读到这可能与我在异步工作程序中运行单线程进程有关,但是我不确定套接字是否是单线程进程。 Also I am not entirely sure how I am supposed to deal with exceptions in a backgroundworker so I just let it fall back to the RunWorkerCompletedEventArgs then retrieve the error message from there. 另外,我也不完全确定应该如何处理backgroundworker中的异常,因此我只是让它退回到RunWorkerCompletedEventArgs,然后从那里检索错误消息。

What I have tried: 我试过的
- I tried putting try catches every where then removing try catches nothing seems to be able to capture this error -我尝试将try catchs放在所有位置,然后删除try catchs似乎无法捕获此错误
- I checked my systems event log and nothing is showing up except my restarts after my computer freezes -我检查了系统事件日志,除了计算机死机后重新启动外,什么都没有显示
- I have attempted to isolate the issue but it can literally happen at any point from the program starting till when I attempt to connect -我试图找出问题所在,但是从程序开始到尝试连接之前,它确实可能在任何时候发生

Setup: 设定:
I am running the program out of visual studio 2012 professional on a windows 8 pro machine. 我正在Windows 8 Pro机器上运行Visual Studio 2012 Professional以外的程序。 The computer I am on has a i7-3770K 3.50GHz and 32GB of ram. 我所在的计算机具有3.50GHz的i7-3770K和32GB的RAM。 The application that is attempting to make a connection to mine is a Android application and the credentials are incorrect when it is attempting to connect. 尝试建立连接的应用程序是Android应用程序,并且尝试连接时凭据不正确。 Visual Studio is running off my main hard drive and building the project on another drive. Visual Studio正在运行我的主硬盘,并在另一个驱动器上构建项目。

Closing: 闭幕:
With all that said would some one please be willing to help me? 话虽如此,请问有人愿意帮助我吗? If you need any more information I will be happy to provide it, please ask. 如果您需要更多信息,我们很乐意提供。

Main Method: 主要方法:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Data;
 using System.Windows.Documents;
 using System.Windows.Input;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
 using System.Windows.Navigation;
 using System.Windows.Shapes;
 using System.ComponentModel;

namespace Server
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class SourceServer : Window
{
    private BackgroundWorker worker = new BackgroundWorker();

    public SourceServer()
    {
        InitializeComponent();

        StartListeningForConnections();
    }

    private void StartListeningForConnections()
    {

        worker.DoWork += new DoWorkEventHandler(worker_DoWork);
        worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
        worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
        worker.WorkerReportsProgress = true;

        if (worker.IsBusy != true)
        {
            worker.RunWorkerAsync();
        }
    }

    private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        worker.ReportProgress(0, "Source server version 0.0.0.1ib started");
        LoginServer oLoginServer = new LoginServer();
        oLoginServer.StartListening(worker);

    }

    private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        try
        {
            lvOutput.Items.Add(e.UserState.ToString());
        }
        catch (Exception exception)
        {
            lvOutput.Items.Add(exception.StackTrace);
        }
    }

    private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            System.IO.File.WriteAllText(Environment.CurrentDirectory + @"\log.txt", e.Error.StackTrace + " /n " + e.Error.Message);
        }
        else
        {
            MessageBox.Show("Error was null");
        }
        worker.Dispose();
    }
}
}

SSL Socket Connection: SSL套接字连接:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Threading;
 using System.Net;
 using System.Net.Sockets;
 using System.Windows;
 using System.Windows.Controls;
 using System.ComponentModel;
 using System.Net.Security;
 using System.Security.Cryptography.X509Certificates;
 using MySql.Data.MySqlClient;
 using System.IO;

namespace Server
{
public class LoginServer
{
    // Incoming data from the client.
    public static string data = null;
    public static X509Certificate serverCertificate = null;

    public delegate void UpdateListView(ListView oOutput);

    public void StartListening(BackgroundWorker worker)
    {
        // Data buffer for incoming data.
        byte[] bytes = new Byte[1024];

        // Establish the local endpoint for the socket.
        IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
        IPAddress ipAddress = ipHostInfo.AddressList[1];

        serverCertificate = X509Certificate.CreateFromCertFile(@"server.crt");
        TcpListener oServer = new TcpListener(ipAddress, 12345);


        // Bind the socket to the local endpoint and 
        // listen for incoming connections.

        // Start listening for connections.
        while (true)
        {
            Thread.Sleep(100);
            worker.ReportProgress(0, "Waiting for connection....");

            // Program is suspended while waiting for an incoming connection.
            //Socket handler = listener.Accept();
            oServer.Start();
            TcpClient oClient = oServer.AcceptTcpClient();
            Stream oStream = oClient.GetStream();
            SslStream oSSLStream = new SslStream(oStream);

            data = null;

            // An incoming connection needs to be processed.
            string sUsername = "place holder";
            string sPassword = "place holder";

            while (true)
            {
                bytes = new byte[1024];
                int bytesRec = oSSLStream.Read(bytes, 0, bytes.Length);
                data += Encoding.ASCII.GetString(bytes, 0, bytesRec);

                string[] sCredentials = data.Split("|".ToCharArray()[0]);

                sUsername = sCredentials[0];
                sPassword = sCredentials[1];

                if (data.IndexOf("<EOF>") > -1)
                {
                    break;
                }
            }

            // Show the data on the console.
            worker.ReportProgress(0, "Connection Recieved : ");
            worker.ReportProgress(0, "Username: " + sUsername);
            worker.ReportProgress(0, "Password: " + sPassword);
            worker.ReportProgress(0, "");

            // Echo the data back to the client.
            byte[] msg;
            if (sUsername.Equals("test") && sPassword.Equals("test"))
            {
                msg = Encoding.ASCII.GetBytes("approved<EOF>\n");
                worker.ReportProgress(0, "approved");
                oSSLStream.Write(msg, 0, msg.Length);
            }
            else
            {
                msg = Encoding.ASCII.GetBytes("rejected<EOF>\n");
                worker.ReportProgress(0, "rejected");
                oSSLStream.Write(msg, 0, msg.Length);
            }
        }
    }

    public void VerifyUser()
    {

    }
}
}

While I don't see any reason for this to lock up your entire computer, I do see a couple of reasons for the application to potentially hang... 虽然我看不到有什么原因可以锁定您的整个计算机,但确实有一些原因导致应用程序可能挂起...

Your while loop inside of your SSL server will never break unless your client writes 除非您的客户写信,否则SSL服务器内部的while循环将永不中断。

'<EOF>'
to the stream; 到溪流; which you would have to force it to do. 您将不得不强制执行该操作。 I would likely do something similar to this: 我可能会做类似的事情:

 while(( bytesRec = oSSLStream.Read(bytes,0,bytes.Length)) > 0 ) { // Compare input & break } 

-- The while loop you have now ( without a thread sleep ) will consume all of your systems resources waiting for ... something that may never occur. -您现在拥有的while循环(没有线程睡眠)将消耗所有系统资源,以等待...某些可能永远不会发生的事情。

In a related issue - I note that your 'DoWork' method launches the listener - but does not start a new thread for this listener. 在一个相关的问题中-我注意到您的'DoWork'方法将启动侦听器-但不会为此侦听器启动新线程。 This means that the listener is running inside of your interface thread - which will cause the interface ( and potentially more... ) to hang until the process is completed - which as stated, may never happen. 这意味着侦听器正在您的接口线程内运行-这将导致该接口(可能还有更多...)挂起,直到该过程完成为止-如上所述,这可能永远不会发生。

... Ahem... This last paragraph may be incorrect - you are running an async worker, so I may be incorrect in my second assessment. ...糟糕...上一段可能不正确-您正在运行异步工作程序,因此第二次评估中我可能不正确。

Cheers, hope this is helpful. 干杯,希望这会有所帮助。

I've had some hanging problems on Windows 8 that I never saw on Windows 7 (with VS2012). 我在Windows 8上遇到了一些挂起的问题,而在Windows 7(使用VS2012)上却从未见过。 As you experienced it worked fine the first time but only locked up Visual Studio (and not my whole machine) and I had to force quit. 如您所见,它第一次运行良好,但是只锁定了Visual Studio(而不是我的整个计算机),因此我不得不强行退出。

The Visual Studio 2012 Update 4 (which focuses on bug fixes and compatibility) seemed to fix it, although I didn't scientifically test this. 尽管我没有科学地对此进行测试,但Visual Studio 2012 Update 4 (专注于错误修复和兼容性)似乎已对其进行了修复。

Note: As of 9/1/13 this is only the RC2 version so please check for newer versions, and edit this answer when RTM happens. 注意:从13年9月1日开始,这仅是RC2版本,因此请检查是否有较新版本,并在发生RTM时编辑此答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#WPF更新UI应用程序冻结 - C# WPF Update UI Application freeze 在C#中循环播放大量图像时计算机冻结-WPF - Computer freeze when looping a large list of images in c# - Wpf 我可以在租用/云计算机上运行C#.exe吗? - Can I run my C# .exe on a rented / cloud computer? 在其他计算机中自动调整大小的应用程序WPF C# - auto resize application in other computer wpf c# C#WPF应用程序在已开发的计算机上运行,​​但在其他任何地方都无法运行 - C# WPF Application runs on the developed Computer but not any where else WPF 应用程序在一个循环中,如何不让整个应用程序冻结? - WPF application in a loop, how to not have the whole application freeze? 我如何在远程计算机上运行应用程序并使用.net C#等待结果 - How can i run an application in remote computer and wait for results using .net c# 我是否需要在计算机上设置sql server才能运行vs 10中制作的c#桌面应用程序文件? - DO I need to setup sql server in a computer to run c# desktop application file made in vs 10? C# 应用程序无法在另一台计算机上运行 - C# application doesn't run on another computer 在计算机上运行ac#控制台应用程序需要什么 - What is required to run a c# console application on a computer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM