internal HeidenhainDNC(string ipAddress) { address = ipAddress; // Create the machine object #if INPROC m_machine = new HeidenhainDNCLib.JHMachineInProcess(); #else m_machine = new HeidenhainDNCLib.JHMachine(); #endif //string name = FindConnection("127.0.0.1"); }
// Break the DNC connection private void Disconnect() { /* MSDN: How to: Handle Events Raised by a COM Source * Note that COM objects that raise events within a .NET client require two Garbage Collector (GC) collections before they are released. * This is caused by the reference cycle that occurs between COM objects and managed clients. * If you need to explicitly release a COM object you should call the Collect method twice. * * This is important for COM events that pass COM objects as arguments, s.a. JHError::OnError2. * If the argument was not explicitly released, it will hold a reference to the parent JHError object, thus preventing a successful Disconnect(). */ System.GC.Collect(); System.GC.Collect(); if (m_automatic != null) { #if NOTUSED // Unhook the OnCOMProgramStateChanged event handler from the OnProgramStatusChanged event //((HeidenhainDNCLib._IJHAutomaticEvents3_Event)m_automatic).OnProgramStatusChanged -= ((HeidenhainDNCLib._IJHAutomaticEvents2_Event)m_automatic).OnProgramStatusChanged -= new HeidenhainDNCLib._IJHAutomaticEvents_OnProgramStatusChangedEventHandler(OnCOMProgramStateChanged); ((HeidenhainDNCLib._IJHAutomaticEvents3_Event)m_automatic).OnToolChanged -= new HeidenhainDNCLib._IJHAutomaticEvents2_OnToolChangedEventHandler(OnCOMToolChanged); ((HeidenhainDNCLib._IJHAutomaticEvents3_Event)m_automatic).OnDncModeChanged -= new HeidenhainDNCLib._IJHAutomaticEvents_OnDncModeChangedEventHandler(OnCOMDncModeChanged); ((HeidenhainDNCLib._IJHAutomaticEvents3_Event)m_automatic).OnProgramChanged -= new HeidenhainDNCLib._IJHAutomaticEvents2_OnProgramChangedEventHandler(OnCOMProgramChanged); ((HeidenhainDNCLib._IJHAutomaticEvents3_Event)m_automatic).OnExecutionMessage -= new HeidenhainDNCLib._IJHAutomaticEvents2_OnExecutionMessageEventHandler(OnCOMExecutionMessage); #endif // Explicitly release the JHAutomatic COM object, to allow a successful disconnect. System.Runtime.InteropServices.Marshal.ReleaseComObject(m_automatic); m_automatic = null; } try { m_machine.Disconnect(); // Explicitly release the JHMachine COM object, to allow a successful new connection. System.Runtime.InteropServices.Marshal.ReleaseComObject(m_machine); m_machine = null; } catch (Exception) { // Ignore errors } }