Example #1
0
        /**
         * Clean up an exit the program.
         *
         * @param acq Acquisition handle.
         * @param callbackTpye Registered callback.
         * @param waiting Activate waiting for user interaction.
         */
        static void exitProgram(ref HFramegrabber acq, string callbackType,
                                bool waiting)
        {
            if (waiting)
            {
                waitUserInteraction();
            }

            if (null == acq)
            {
                System.Environment.Exit(1);
            }

            // Unregister the callback.
            if (null != callbackType)
            {
                try
                {
                    IntPtr currentCallback, context;
                    currentCallback =
                        acq.GetFramegrabberCallback(callbackType, out context);
                    // Already unregistered?
                    if (IntPtr.Zero != currentCallback)
                    {
                        acq.SetFramegrabberCallback(callbackType, IntPtr.Zero, IntPtr.Zero);
                    }
                }
                catch (HalconException exc)
                {
                    Console.WriteLine(exc.GetErrorMessage());
                }
            }

            // Abort the workerThread.
            if (gThreadRunning)
            {
                try
                {
                    // abort the current grab.
                    acq.SetFramegrabberParam("do_abort_grab", 1);
                }
                catch (HalconException exc)
                {
                    Console.WriteLine(exc.GetErrorMessage());
                }
                gAcqMutex.LockMutex();
                gThreadRunning = false;
                gAcqCondition.SignalCondition();
                gAcqMutex.UnlockMutex();
                gThreadHandle.Join();
            }

            try
            {
                // Close the framegrabber.
                acq.Dispose();
                // Clean up.
                gAcqMutex.Dispose();
                gAcqCondition.Dispose();
            }
            catch (HalconException exc)
            {
                Console.WriteLine(exc.GetErrorMessage());
            }

            System.Environment.Exit(1);
        }