The WPF C # application will freeze my entire computer for 2-3 times. I run it

I put a lot of information into this problem because I have no idea what relavent will be

Question:
I have a problem with the program that I work on, when they start it, it freezes my entire computer and does not return an error (I am completely unable to do something that CTRL + ALT + DEL does not even work). This program accepts the connection from the android client and the atm Android client is not configured correctly, so the connection is rejected.

Question:
How can I stop my program from freezing my entire machine?

Hypothesis:
I have several theories regarding what is happening, but I don’t know how to fix them. I read that this may have something to do with me when I start a single-threaded process inside my asynchronous worker, but I'm not sure if a socket is a single-threaded process. Also, I'm not quite sure how I should deal with exceptions in backgroundworker, so I just let it go back to RunWorkerCompletedEventArgs and then it gets an error message.

What I tried:
- I tried to set try catch every time, and then delete attempts to catch anything, it seems that I am not able to fix this error
- I checked the system event log and nothing appears, except for my reboots after my computer freezes
- I tried to isolate the problem, but it can happen literally at any time from the program, starting from the moment when I try to connect to

Setup:
I run the program from the professional studio Visual Studio 2012 on a computer with Windows 8 pro. The computer I work on has an i7-3770K 3.50 GHz and 32 GB of RAM. An application trying to establish a connection with mine is an Android application, and the credentials are incorrect when it tries to connect. Visual Studio disconnects my primary hard drive and creates a project on another drive.

Closure:
With all that said, does anyone please agree to help me? If you need more information, I will be happy to provide it, please ask.

The 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:

  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() { } } } 
+4
source share
2 answers

While I see no reason to lock your entire computer, I see several reasons why the application may freeze ...

Your while loop inside your SSL server will never be interrupted unless your client writes

  '<EOF>' 
into the stream; which you would have to make him do. I would rather do something similar to this:
 while(( bytesRec = oSSLStream.Read(bytes,0,bytes.Length)) > 0 ) { // Compare input & break } 

- The while loop that you have (without a sleeping thread) will consume all of your system resources waiting ... for something that may never happen.

In a related issue - I note that your DoWork method starts a listener - but does not start a new thread for this listener. This means that the listener works inside your interface thread, which will cause the interface to hang (and potentially more ...) until the process is complete, which, as indicated, may not happen.

... Ahem ... This last paragraph may be wrong - you are working with an asynchronous worker, so I might be wrong in the second assessment.

Hi, hope this will be helpful.

+2
source

I had problems with freezing in Windows 8 that I had never seen in Windows 7 (with VS2012). As you experienced it worked fine for the first time, but only locked Visual Studio (and not my entire machine), and I had to get out.

Update Visual Studio 2012 4 (which focuses on bug fixes and compatibility), it seemed to fix it, although I do not scientifically tested this.

Note: As of 9/1/13, this is only the RC2 version, so please check for new versions and edit this answer when RTM happens.

0
source

Source: https://habr.com/ru/post/1490590/


All Articles