[chronojump] Safer opening image after a R process



commit 1566e90a6cd3e1b5bfa9982af5f8d8b669cb8942
Author: Xavier de Blas <xaviblas gmail com>
Date:   Sat Apr 25 02:58:29 2015 +0200

    Safer opening image after a R process

 src/gui/encoder.cs |   21 +++++++--------------
 src/utilGtk.cs     |   40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 14 deletions(-)
---
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index 6abeb8f..54eb899 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -4955,8 +4955,10 @@ public partial class ChronoJumpWindow
 
                                List<string> contents = 
Util.ReadFileAsStringList(UtilEncoder.GetEncoderCurvesTempFileName());
                                
-                               Pixbuf pixbuf = new Pixbuf (UtilEncoder.GetEncoderGraphTempFileName()); 
//from a file
-                               image_encoder_capture.Pixbuf = pixbuf;
+                               image_encoder_capture = UtilGtk.OpenImageSafe(
+                                               UtilEncoder.GetEncoderGraphTempFileName(),
+                                               image_encoder_capture);
+                               
                                encoderUpdateTreeViewCapture(contents); //this updates encoderCaptureCurves
                                image_encoder_capture.Sensitive = true;
 
@@ -5152,18 +5154,9 @@ public partial class ChronoJumpWindow
                                //o si es una sola i fa alguna edicio
                                
                                //maybe image is still not readable
-                               bool readedOk;
-                               do {
-                                       readedOk = true;
-                                       try {
-                                               Pixbuf pixbuf = new Pixbuf 
(UtilEncoder.GetEncoderGraphTempFileName()); //from a file
-                                               image_encoder_analyze.Pixbuf = pixbuf;
-                                       } catch {
-                                               LogB.Warning("File is still not ready. Wait a bit");
-                                               System.Threading.Thread.Sleep(50);
-                                               readedOk = false;
-                                       }
-                               } while( ! readedOk );
+                               image_encoder_analyze = UtilGtk.OpenImageSafe(
+                                               UtilEncoder.GetEncoderGraphTempFileName(),
+                                               image_encoder_analyze);
 
                                encoder_pulsebar_analyze.Text = "";
 
diff --git a/src/utilGtk.cs b/src/utilGtk.cs
index e4bba76..1b277e9 100644
--- a/src/utilGtk.cs
+++ b/src/utilGtk.cs
@@ -21,6 +21,7 @@
 using System;
 using System.Text; //StringBuilder
 using System.Collections; //ArrayList
+using System.IO;
 using Gtk;
 using Gdk;
 
@@ -479,6 +480,45 @@ public class UtilGtk
                UtilGtk.ErasePaint(da, px);
        }
        
+       /*
+        *
+        * IMAGE
+        *
+        */
+
+       /*
+        * OpenImageSafe checks if it's created and if it has size and can be opened
+        * this is used when one process takes an image from another process and maybe is not finished
+        */
+       public static Gtk.Image OpenImageSafe(string filename, Gtk.Image image) 
+       {
+               while( ! File.Exists(filename) );
+
+               bool hasSize = false;
+               do {
+                       FileInfo fi = new FileInfo(filename);
+                       if(fi.Length > 0)
+                               hasSize = true;
+                       else
+                               System.Threading.Thread.Sleep(50);
+               } while( ! hasSize );
+
+               bool readedOk;
+               do {
+                       readedOk = true;
+                       try {
+                               Pixbuf pixbuf = new Pixbuf (filename); //from a file
+                               image.Pixbuf = pixbuf;
+                       } catch {
+                               LogB.Warning("File is still not ready. Wait a bit");
+                               System.Threading.Thread.Sleep(50);
+                               readedOk = false;
+                       }
+               } while( ! readedOk );
+
+               return image;
+       }
+
 
 
 }


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