/[xmltv_parser]/trunk/libxmltv/Core/XMLTVRuntimeInstance.cs
ViewVC logotype

Diff of /trunk/libxmltv/Core/XMLTVRuntimeInstance.cs

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

--- trunk/libxmltv/Core/XMLTVInstance.cs	2013/03/08 06:39:56	51
+++ trunk/libxmltv/Core/XMLTVInstance.cs	2013/03/08 08:51:10	54
@@ -4,19 +4,32 @@
 using System.Text;
 using libxmltv.Interfaces;
 using System.ComponentModel;
+using System.Windows.Forms;
+using System.Threading;
 
 namespace libxmltv.Core
 {
     [Serializable]
-    internal class XMLTVRuntimeInstance : MarshalByRefObject, IXMLTVRuntimeInstance, ISerializer<XMLTVRuntimeInstance>
+    internal class XMLTVRuntimeInstance : MarshalByRefObject, IXMLTVRuntimeInstance, ISerializer<XMLTVRuntimeInstance>, IGetCreatedInstanceEvent, ISetCreatedInstanceEvent, IDestroyInstance
     {
-        public XMLTVRuntimeInstance(string xmlfile) : this(xmlfile, null) { }
-        public XMLTVRuntimeInstance(string xmlfile, EventHandler<CancelEventArgs> t) { CreateInstance(xmlfile,t); }
-        private void CreateInstance(string xmlfile, EventHandler<CancelEventArgs> t)
+        Thread worker;
+        public XMLTVRuntimeInstance(string xmlfile) 
         {
-            CancelEvent = t;
-            using (XMLTVInstance instance = new XMLTVInstance(xmlfile, this))
+            worker = new Thread(new ParameterizedThreadStart(CreateInstance));
+            //CreateInstance(xmlfile);
+            worker.Start(xmlfile);
+        }
+        //public XMLTVRuntimeInstance(string xmlfile) : this(xmlfile, null) { }
+        //public XMLTVRuntimeInstance(string xmlfile, EventHandler<CancelEventArgs> t) { CreateInstance(xmlfile,t); }
+        private void CreateInstance(object xmlfile)
+        {            
+            //CancelEvent = t;
+            using (XMLTVInstance instance = new XMLTVInstance(xmlfile.ToString(), this))
             {
+                if (OnInstanceCreated != null)
+                {
+                    OnInstanceCreated.Invoke(this,new EventArgs());
+                }
             }
         }
 
@@ -32,39 +45,51 @@
         public Dictionary<int, IXMLTVProgram> Programs { get; set; }
         #endregion   
     
+        public EventHandler<EventArgs> OnInstanceCreated { get; set; }
+        public EventHandler<EventArgs> GetOnInstanceCreated() { return OnInstanceCreated; }
+        public void SetOnInstanceCreated(EventHandler<EventArgs> event_instance) { OnInstanceCreated = event_instance; }
+
         public IXMLTVSerializer<XMLTVRuntimeInstance> Serializer
         {
-            get { return new XMLTVSerializer<XMLTVRuntimeInstance>(this); }
+            get 
+            {
+                ///* We have to set CancelEvent to null, before returning a new instance of the serializer otherwise all subscribers to the event will have to be marked as serializeable.
+                //   Most subscribers will be of type: System.Windows.Forms which is not marked as serializable and will fail to serialize. */
+                //if (CancelEvent != null) { CancelEvent = null; }
+                return new XMLTVSerializer<XMLTVRuntimeInstance>(this); 
+            }
         }
-        //public void Dispose()
-        //{
-        //    IsDisposing = true;
-        //    //throw new NotImplementedException();
-        //}
-        //public bool IsDisposing { get; private set; }
-
-
-        private event EventHandler<CancelEventArgs> CancelEvent = null;
-
-        public bool IsAborting
+        public void DestroyInstance()
         {
-            get
+            xmltv_logger.Log.Debug.WriteLine("Destoying Instance of: '{0}'", this.GetType().Name);
+            this.IsAborting = true;
+            if (worker == null)
+            {
+                xmltv_logger.Log.Debug.WriteLine("Unable to destroy instance of: '{0}' - worker thread is null", this.GetType().Name);
+                return;
+            }
+            else
             {
-                if (CancelEvent != null)
+                if (worker.IsAlive)
                 {
-                    CancelEventArgs e = new CancelEventArgs();
-                    CancelEvent.Invoke(this, e);
-                    if (e.Cancel)
+                    xmltv_logger.Log.Verbose.Debug.WriteLine("Requesting Instance to Abort...");
+                    while (worker.IsAlive)
                     {
-                        xmltv_logger.Log.Verbose.Debug.WriteLine("Detected Instance abort event...");
+                        worker.Abort();
+                        Application.DoEvents();
                     }
-                    return e.Cancel;
                 }
-                return false;
+                else
+                {
+                    xmltv_logger.Log.Debug.WriteLine("Instance of: '{0}'- already destroyed.", this.GetType().Name);
+                }
             }
         }
-
-
+        public bool IsAborting
+        {
+            get;
+            private set;
+        }        
     }
 
     internal class XMLTVInstance : IDisposable

 

  ViewVC Help
Powered by ViewVC 1.1.22