/[xmltv_parser]/trunk/xmltv_parser/main.cs
ViewVC logotype

Diff of /trunk/xmltv_parser/main.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/xmltv_parser/main.cs	2013/03/08 07:01:37	52
+++ trunk/xmltv_parser/main.cs	2013/03/08 08:51:10	54
@@ -18,7 +18,7 @@
 
     public partial class main : Form
     {
-        bool form_closing = false;
+        //bool form_closing = false;
         //private IXMLTVRuntimeInstance xmltv;
         private bool IsUnix
         {
@@ -34,33 +34,10 @@
         }
 
         StringBuilder log_flusher = new StringBuilder();
-        bool HAVE_CR = false;
+        
         void Log_OnFlush(object sender, LoggerOnFlushEventArgs e)
         {
-
-            log_flusher.Append(e.Buffer);
-            if (IsUnix)
-            {
-                if (e.Buffer == System.Environment.NewLine[0])
-                {
-                    OnLogFlush(log_flusher.ToString());
-                    log_flusher = new StringBuilder();
-                }
-            }
-            else
-            {
-                if (e.Buffer == System.Environment.NewLine[0])
-                {
-                    HAVE_CR = true;
-                }
-                if (e.Buffer == System.Environment.NewLine[1] && HAVE_CR)
-                {
-                    OnLogFlush(log_flusher.ToString().TrimStart(new char[] { System.Environment.NewLine[1] }));
-                    log_flusher = new StringBuilder();
-                    HAVE_CR = false;
-                }
-            }
-
+            OnLogFlush(e.Buffer);
         }
         //bool txtLog_EnterMouse = false;
 
@@ -76,16 +53,25 @@
         {
             if (this.IsDisposed) { return; }
             Console.Write(logmessage);
+            UpdateStatus(logmessage);
+            UpdateLogOutput(logmessage);
+            Application.DoEvents();
+        }
+
+        void UpdateStatus(string logmessage)
+        {            
             txtStatus.Text = logmessage.Replace(System.Environment.NewLine, "");
+        }
+        void UpdateLogOutput(string logmessage)
+        {
+            if (txtLog.InvokeRequired)
+            {
+                txtLog.Invoke((Action)(delegate { UpdateLogOutput(logmessage); }));
+                return;
+            }
             txtLog.AppendText(logmessage);
-
-            //if (!txtLog_EnterMouse)
-            //{
             txtLog.SelectionStart = txtLog.Text.Length; //Set the current caret position to the end
             txtLog.ScrollToCaret(); //Now scroll it automatically
-            //}
-            //Thread.Sleep(100);
-            Application.DoEvents();
         }
 
         //List<IXMLTVChannel> Channels;
@@ -103,23 +89,40 @@
 
         void LoadXMLTVShcedule(string schedule_xml)
         {
-            XMLTV.CreateInstance(schedule_xml, new EventHandler<CancelEventArgs>(xmltv_cancelevent));
-            var program_count = XMLTV.GetInstance().Programs.Values.Count;
-            var program_list = XMLTV.GetInstance().Programs.Values.ToList().OrderBy(s => s.Start);
-            //Programs = program_list(0, program_count).ToList();
-            Programs = new List<IXMLTVProgram>(program_list.ToArray());
+            //XMLTV.CreateInstance(schedule_xml, new EventHandler<CancelEventArgs>(xmltv_cancelevent));
+            XMLTV.CreateInstance(schedule_xml);
+            XMLTV.OnInstanceCreated += new EventHandler<EventArgs>(XMLTV_OnInstanceCreated);           
         }
 
-        void xmltv_cancelevent(object sender, CancelEventArgs e)
+        void XMLTV_OnInstanceCreated(object sender, EventArgs e)
         {
-            if (form_closing)
+            var instance = XMLTV.GetInstance();
+            if (instance != null)
             {
-                e.Cancel = true;
+                var program_count = instance.Programs.Values.Count;
+                var program_list = instance.Programs.Values.ToList().OrderBy(s => s.Start);
+                //Programs = program_list(0, program_count).ToList();
+                Programs = new List<IXMLTVProgram>(program_list.ToArray());
             }
+            CreateControls();
         }
 
+
+        //void xmltv_cancelevent(object sender, CancelEventArgs e)
+        //{
+        //    if (form_closing)
+        //    {
+        //        e.Cancel = true;
+        //    }
+        //}
+
         void CreateControls()
         {
+            if (this.InvokeRequired)
+            {
+                this.Invoke((Action)(delegate { CreateControls(); }));
+                return;
+            }
             foreach (var program in Programs)
             {
                 ListViewItem li = new ListViewItem(string.Format("{0} {1}", program.Channel.Number, program.Channel.CallSign));
@@ -141,7 +144,7 @@
                 if (result != DialogResult.OK) return;
                 ClearLocalLog();
                 LoadXMLTVShcedule(xmltv_file_chooser.FileName);
-                CreateControls();
+                //CreateControls();
             }
             catch (Exception ex)
             {
@@ -195,7 +198,11 @@
 
         private void main_FormClosing(object sender, FormClosingEventArgs e)
         {
-            form_closing = true;
+            try
+            {
+                XMLTV.DestroyInstance();
+            }
+            catch { }
         }
 
         private void mnuItemClearLocalLog_Click(object sender, EventArgs e)

 

  ViewVC Help
Powered by ViewVC 1.1.22