--- trunk/libxmltv/Core/XMLTVInstance.cs 2013/03/08 03:36:44 44 +++ trunk/libxmltv/Core/XMLTVInstance.cs 2013/03/08 10:19:32 55 @@ -3,18 +3,41 @@ using System.Linq; using System.Text; using libxmltv.Interfaces; +using System.ComponentModel; +using System.Windows.Forms; +using System.Threading; namespace libxmltv.Core { - internal class XMLTVRuntimeInstance : MarshalByRefObject, IXMLTVRuntimeInstance, IDisposable + [Serializable] + internal class XMLTVRuntimeInstance : MarshalByRefObject, IXMLTVRuntimeInstance, ISerializer, IGetCreatedInstanceEvent, ISetCreatedInstanceEvent, IDestroyInstance { - public XMLTVRuntimeInstance(string xmlfile) { CreateInstance(xmlfile); } - private void CreateInstance(string xmlfile) { Instance = new XMLTVInstance(xmlfile, this); } - internal XMLTVInstance Instance { get; private set; } + 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 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()); + } + } + } + + //internal XMLTVInstance Instance { get; private set; } #region IXMLTV_LOADER members public System.IO.FileInfo XmlFile { get; set; } - public System.Xml.Linq.XDocument XmlDoc { get; set; } + public string XmlDoc { get; set; } #endregion #region IXMLTV_PARSER Members public IXMLTVSource Source { get; set; } @@ -22,12 +45,51 @@ public Dictionary Programs { get; set; } #endregion - public void Dispose() + public EventHandler OnInstanceCreated { get; set; } + public EventHandler GetOnInstanceCreated() { return OnInstanceCreated; } + public void SetOnInstanceCreated(EventHandler event_instance) { OnInstanceCreated = event_instance; } + + public IXMLTVSerializer Serializer + { + 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(this); + } + } + public void DestroyInstance() { - IsDisposing = true; - //throw new NotImplementedException(); + xmltv_logger.Debug.WriteLine("Destoying Instance of: '{0}'", this.GetType().Name); + this.IsAborting = true; + if (worker == null) + { + xmltv_logger.Debug.WriteLine("Unable to destroy instance of: '{0}' - worker thread is null", this.GetType().Name); + return; + } + else + { + if (worker.IsAlive) + { + xmltv_logger.Verbose.Debug.WriteLine("Requesting Instance to Abort..."); + while (worker.IsAlive) + { + worker.Abort(); + Application.DoEvents(); + } + } + else + { + xmltv_logger.Debug.WriteLine("Instance of: '{0}'- already destroyed.", this.GetType().Name); + } + } } - public bool IsDisposing { get; private set; } + public bool IsAborting + { + get; + private set; + } } internal class XMLTVInstance : IDisposable