[chronojump] Better abort of ping



commit 434917cad82d9789e60a61a653a588bb5081c772
Author: Xavier de Blas <xaviblas gmail com>
Date:   Tue Dec 13 15:00:25 2016 +0100

    Better abort of ping

 src/gui/chronojump.cs |   28 ++++++++++++++++++----------
 src/json.cs           |   34 +++++++++++++++++++++++++++++-----
 2 files changed, 47 insertions(+), 15 deletions(-)
---
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index ac5c04c..fb5c644 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -2294,6 +2294,15 @@ public partial class ChronoJumpWindow
                */
                cp2016.SerialPortsCloseIfNeeded();
 
+               //exit start ping if has not ended
+               if(pingThread.IsAlive)
+               {
+                       LogB.Information("Closing ping thread");
+                       //pingThread.Abort();
+                       jsPing.PingAbort();
+               }
+
+
                //printing remaining logs in the non-gtk thread
                LogB.Information("Printing non-GTK thread remaining log");
                LogB.Information(LogSync.ReadAndEmpty());
@@ -2315,10 +2324,6 @@ public partial class ChronoJumpWindow
                encoderRProcAnalyze.SendEndProcess();
 
                LogB.Information("Bye3!");
-
-               //exit start ping if has not ended
-               if(pingThread.IsAlive)
-                       pingThread.Abort();
                
                Log.End();
 
@@ -6575,26 +6580,29 @@ LogB.Debug("X");
        {
                pingDo(false);
        }
+
+       //declared here in order to be easy closed on exit
+       Json jsPing;
        private void pingDo(bool showInWindow)
        {
-               Json js = new Json();
-               bool success = js.Ping(UtilAll.GetOS(), UtilAll.ReadVersionFromBuildInfo(), 
preferences.machineID);
+               jsPing = new Json();
+               bool success = jsPing.Ping(UtilAll.GetOS(), UtilAll.ReadVersionFromBuildInfo(), 
preferences.machineID);
 
                if(success) {
-                       LogB.Information(js.ResultMessage);
+                       LogB.Information(jsPing.ResultMessage);
                        if(showInWindow)
                                new DialogMessage(
                                                "Chronojump",
                                                Constants.MessageTypes.INFO, 
-                                               js.ResultMessage);
+                                               jsPing.ResultMessage);
                }
                else {
-                       LogB.Error(js.ResultMessage);
+                       LogB.Error(jsPing.ResultMessage);
                        if(showInWindow)
                                new DialogMessage(
                                                "Chronojump",
                                                Constants.MessageTypes.WARNING, 
-                                               js.ResultMessage);
+                                               jsPing.ResultMessage);
                }
 
                /*
diff --git a/src/json.cs b/src/json.cs
index 01361e6..2734ff3 100644
--- a/src/json.cs
+++ b/src/json.cs
@@ -185,16 +185,31 @@ public class Json
                return true;
        }
 
+       /*
+        * if software just started, ping gets stuck by network problems, and user try to exit software,
+        * thread.Abort doesn't kill the thread properly
+        * just kill the webRequest
+        */
+       WebRequest requestPing;
+       bool requestPingAborting;
+
+       public void PingAbort()
+       {
+               requestPingAborting = true;
+               requestPing.Abort(); //cancel an asynchronous request
+       }
        public bool Ping(string osVersion, string cjVersion, string machineID) 
        {
+               requestPingAborting = false;
+
                // Create a request using a URL that can receive a post. 
-               WebRequest request = WebRequest.Create (serverUrl + "/ping");
+               requestPing = WebRequest.Create (serverUrl + "/ping");
 
                // Set the Method property of the request to POST.
-               request.Method = "POST";
+               requestPing.Method = "POST";
 
                // Set the ContentType property of the WebRequest.
-               request.ContentType = "application/json";
+               requestPing.ContentType = "application/json";
 
                // Creates the json object
                JsonObject json = new JsonObject();
@@ -208,13 +223,18 @@ public class Json
                // Writes the json object into the request dataStream
                Stream dataStream;
                try {
-                       dataStream = request.GetRequestStream ();
+                       dataStream = requestPing.GetRequestStream ();
                } catch {
                        this.ResultMessage = 
                                string.Format(Catalog.GetString("You are not connected to the Internet\nor 
{0} server is down."), 
                                serverUrl);
                        return false;
                }
+               if(requestPingAborting) {
+                       LogB.Information("Aborted from PingAbort");
+                       return false;
+               }
+
                dataStream.Write (Encoding.UTF8.GetBytes(js), 0, js.Length);
 
                dataStream.Close ();
@@ -222,13 +242,17 @@ public class Json
                // Get the response.
                WebResponse response;
                try {
-                       response = request.GetResponse ();
+                       response = requestPing.GetResponse ();
                } catch {
                        this.ResultMessage = 
                                string.Format(Catalog.GetString("You are not connected to the Internet\nor 
{0} server is down."), 
                                serverUrl);
                        return false;
                }
+               if(requestPingAborting) {
+                       LogB.Information("Aborted from PingAbort");
+                       return false;
+               }
 
                // Display the status (will be 201, CREATED)
                Console.WriteLine (((HttpWebResponse)response).StatusDescription);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]