Esempio n. 1
0
        /**
         * Register the callback type selected by the user.
         *
         * @param acq Reference to an acquisition interface.
         * @param callbackType Selected callback to be registered.
         * @param userContext Object to be used at the other side of the callback.
         * @return If the registration was sucessfull.
         */
        static bool registerCallbackType(ref HFramegrabber acq, HTuple callbackType,
                                         object userContext)
        {
            IntPtr contextPtr, funcPtr;

            funcPtr    = Marshal.GetFunctionPointerForDelegate(gDelegateCallback);
            contextPtr = Marshal.AllocHGlobal(Marshal.SizeOf(userContext));
            Marshal.StructureToPtr(userContext, contextPtr, false);

            try
            {
                // If the user context is not needed, IntPtr.Zero can be passed.
                acq.SetFramegrabberCallback(callbackType.S.ToString(), funcPtr,
                                            contextPtr);
            }
            catch (HalconException exc)
            {
                Console.WriteLine(System.Environment.NewLine + "Callback " +
                                  callbackType.S.ToString() + " registration failed!");
                Console.WriteLine(exc.GetErrorMessage());
                return(false);
            }

            return(true);
        }
Esempio n. 2
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);
        }