[chronojump] try/catch on LogB.Commit by multithreading problems of Console.SetOut



commit bf8b9c24fcdfd2f562abf7ccb4fcb0974d5b78c0
Author: Xavier de Blas <xaviblas gmail com>
Date:   Thu Aug 4 13:55:31 2016 +0200

    try/catch on LogB.Commit by multithreading problems of Console.SetOut

 src/encoderRProc.cs |    8 +++++++-
 src/logB.cs         |   36 ++++++++++++++++++++++++------------
 2 files changed, 31 insertions(+), 13 deletions(-)
---
diff --git a/src/encoderRProc.cs b/src/encoderRProc.cs
index 5356d9e..3a10375 100644
--- a/src/encoderRProc.cs
+++ b/src/encoderRProc.cs
@@ -307,7 +307,13 @@ public class EncoderRProcCapture : EncoderRProc
        //here curve is sent compressed (string. eg: "0*5 1 0 -1*3 2")
        public void SendCurve(string curveCompressed)
        {
-               LogB.Information("curveSend [displacement array]",curveCompressed);
+               /*
+                * curveCompressed print has made crash Chronojump once.
+                * Seems to be a problem with multithreading and Console.SetOut, see logB Commit (added a 
try/catch there)
+                * until is not fixed, better leave this commented
+                */
+               //LogB.Information("curveSend [displacement array]",curveCompressed);
+
                p.StandardInput.WriteLine(curveCompressed);     //this will send some lines because 
compressed data comes with '\n's
                p.StandardInput.WriteLine("E");         //this will mean the 'E'nd of the curve. Then data 
can be uncompressed on R
        }       
diff --git a/src/logB.cs b/src/logB.cs
index 8a88c99..1e32bf6 100644
--- a/src/logB.cs
+++ b/src/logB.cs
@@ -188,18 +188,30 @@ public static class LogB
                                        thread_name += "     ";
                        }
 
-                       Console.Write("[{5}{0} {1:00}:{2:00}:{3:00}.{4:000}]", TypeString(type), 
DateTime.Now.Hour,
-                                       DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond, 
thread_name);
-
-                       ConsoleCrayon.ResetColor();
-
-                       if(details != null) {
-                               Console.WriteLine(" {0} - {1}", message, details);
-                       } else {
-                               if(type == LogEntryType.Debug)
-                                       Console.Write(" {0}", message);
-                               else
-                                       Console.WriteLine(" {0}", message);
+                       /*
+                        * TODO: Console.Write is written to a file. see log.Start() at log.cs
+                        * is done by 
+                        *      System.Console.SetOut(sw);
+                        *      System.Console.SetError(sw);
+                        *      sw.AutoFlush = true;
+                        * and this is not threadsafe. 
+                        * Have to find a way using TextWriter.Synchronized http://stackoverflow.com/a/9539571
+                        */
+                       try {
+                               Console.Write("[{5}{0} {1:00}:{2:00}:{3:00}.{4:000}]", TypeString(type), 
DateTime.Now.Hour,
+                                               DateTime.Now.Minute, DateTime.Now.Second, 
DateTime.Now.Millisecond, thread_name);
+
+                               ConsoleCrayon.ResetColor();
+
+                               if(details != null) {
+                                       Console.WriteLine(" {0} - {1}", message, details);
+                               } else {
+                                       if(type == LogEntryType.Debug)
+                                               Console.Write(" {0}", message);
+                                       else
+                                               Console.WriteLine(" {0}", message);
+                               }
+                       } catch (System.IndexOutOfRangeException e) {
                        }
                }
 


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