--- trunk/libxmltv/Core/XMLTVInstance.cs 2013/03/08 07:05:53 53 +++ 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 + internal class XMLTVRuntimeInstance : MarshalByRefObject, IXMLTVRuntimeInstance, ISerializer, 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 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 Programs { get; set; } #endregion + public EventHandler OnInstanceCreated { get; set; } + public EventHandler GetOnInstanceCreated() { return OnInstanceCreated; } + public void SetOnInstanceCreated(EventHandler event_instance) { OnInstanceCreated = event_instance; } + public IXMLTVSerializer Serializer { get @@ -43,36 +59,37 @@ 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) + { + 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