// Make the DNC connection
        private void Connect()
        {
            //Text = "VC# Example - Connected to " + ConnectionList.Text;

            // Create the Automatic object and connect the OnProgramStateChanged event handler
            m_automatic = (HeidenhainDNCLib.JHAutomatic)m_machine.GetInterface(HeidenhainDNCLib.DNC_INTERFACE_OBJECT.DNC_INTERFACE_JHAUTOMATIC);
            // Hook up the OnProgramStateChanged event handler to the OnProgramStatusChanged event
            //((HeidenhainDNCLib._IJHAutomaticEvents3_Event)m_automatic).OnProgramStatusChanged +=
            //((HeidenhainDNCLib._IJHAutomaticEvents2_Event)m_automatic).OnProgramStatusChanged +=
            //new HeidenhainDNCLib._IJHAutomaticEvents_OnProgramStatusChangedEventHandler(OnCOMProgramStateChanged);

#if NOTUSED
            ((HeidenhainDNCLib._IJHAutomaticEvents3_Event)m_automatic).OnDncModeChanged +=
                new HeidenhainDNCLib._IJHAutomaticEvents_OnDncModeChangedEventHandler(OnCOMDncModeChanged);

            ((HeidenhainDNCLib._IJHAutomaticEvents3_Event)m_automatic).OnToolChanged +=
                new HeidenhainDNCLib._IJHAutomaticEvents2_OnToolChangedEventHandler(OnCOMToolChanged);

            ((HeidenhainDNCLib._IJHAutomaticEvents3_Event)m_automatic).OnProgramChanged +=
                new HeidenhainDNCLib._IJHAutomaticEvents2_OnProgramChangedEventHandler(OnCOMProgramChanged);

            ((HeidenhainDNCLib._IJHAutomaticEvents3_Event)m_automatic).OnExecutionMessage +=
                new HeidenhainDNCLib._IJHAutomaticEvents2_OnExecutionMessageEventHandler(OnCOMExecutionMessage);
#endif
            // Get the initial program status.
            m_programState = m_automatic.GetProgramStatus();
        }
        // 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
            }
        }