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

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

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

--- trunk/libxmltv/Core/XMLTVInstance.cs	2013/03/08 07:01:37	52
+++ trunk/libxmltv/Core/XMLTVInstance.cs	2013/03/08 08:51:10	54
@@ -4,20 +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) { CreateInstance(xmlfile); }
+        Thread worker;
+        public XMLTVRuntimeInstance(string xmlfile) 
+        {
+            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(string xmlfile)
-        {
+        private void CreateInstance(object xmlfile)
+        {            
             //CancelEvent = t;
-            using (XMLTVInstance instance = new XMLTVInstance(xmlfile, this))
+            using (XMLTVInstance instance = new XMLTVInstance(xmlfile.ToString(), this))
             {
+                if (OnInstanceCreated != null)
+                {
+                    OnInstanceCreated.Invoke(this,new EventArgs());
+                }
             }
         }
 
@@ -33,6 +45,10 @@
         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 
@@ -43,36 +59,37 @@
                 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)
-                //{
-                //    CancelEventArgs e = new CancelEventArgs();
-                //    CancelEvent.Invoke(this, e);
-                //    if (e.Cancel)
-                //    {
-                //        xmltv_logger.Log.Verbose.Debug.WriteLine("Detected Instance abort event...");
-                //    }
-                //    return e.Cancel;
-                //}
-                //return false;
-                return false;
+                if (worker.IsAlive)
+                {
+                    xmltv_logger.Log.Verbose.Debug.WriteLine("Requesting Instance to Abort...");
+                    while (worker.IsAlive)
+                    {
+                        worker.Abort();
+                        Application.DoEvents();
+                    }
+                }
+                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