--- trunk/libxmltv/Core/XMLTVInstance.cs 2013/03/08 05:20:12 49 +++ 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 + internal class XMLTVRuntimeInstance : MarshalByRefObject, IXMLTVRuntimeInstance, ISerializer, IGetCreatedInstanceEvent, ISetCreatedInstanceEvent, IDestroyInstance { - public XMLTVRuntimeInstance(string xmlfile) : this(xmlfile, null) { } - public XMLTVRuntimeInstance(string xmlfile, EventHandler t) { CreateInstance(xmlfile,t); } - private void CreateInstance(string xmlfile, EventHandler 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 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,42 +45,51 @@ public Dictionary Programs { get; set; } #endregion - internal IXMLTVSerializer Serializer + public EventHandler OnInstanceCreated { get; set; } + public EventHandler GetOnInstanceCreated() { return OnInstanceCreated; } + public void SetOnInstanceCreated(EventHandler event_instance) { OnInstanceCreated = event_instance; } + + public IXMLTVSerializer Serializer { get { - return new XMLTVSerializer(this); + ///* 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 Dispose() - //{ - // IsDisposing = true; - // //throw new NotImplementedException(); - //} - //public bool IsDisposing { get; private set; } - - - private event EventHandler 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) { - if (CancelEvent != null) + xmltv_logger.Log.Debug.WriteLine("Unable to destroy instance of: '{0}' - worker thread is null", this.GetType().Name); + return; + } + else + { + 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