private void _captureThread(object devIdf)
        {
            int devId = (int)devIdf;

            while (keepGoing)
            {
                FrameRate = CalculateFrameRate();

                Thread.Sleep(1);
                if (VIWrapper.IsFrameReady(selectedDeviceIndex))
                {
                    //
                    //    writeLock = true;

                    output = IntPtr.Zero;
                    VIWrapper.GetImage(devId, ref output, false, true);
                    writeLock = false;
                    int width  = VIWrapper.GetWidth(devId);
                    int height = VIWrapper.GetHeight(devId);
                    try
                    {
                        Application.Current.Dispatcher.BeginInvoke(
                            DispatcherPriority.Background,
                            new Action(() =>
                        {
                            CapturedImage = FromNativePointer(output, width, height, 3);
                        }));
                        if (this.runTest)
                        {
                            Stopwatch sw = new Stopwatch();
                            sw.Start();
                            if (CheckIfRed(this.redThreshold, array, width, height, 3))
                            {
                                redDetected.Stop();
                                EndLatencyTest();
                            }
                            sw.Stop();
                            //          Console.WriteLine("It took " + sw.ElapsedTicks + " to run or" + sw.ElapsedMilliseconds + " Ms");
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void _capThread(object deviceID)
        {
            int devId = (int)deviceID;

            while (isCaptureRunning)
            {
                UpdateFrameRate();        //update the framerate.
                Thread.Sleep(1);
                if (isCaptureRunning && VIWrapper.IsFrameReady(devId))
                {
                    unmanagedImagePtr = IntPtr.Zero; // Clean the pointer!
                    try                              //Enter exception safetyland!
                    {
                        VIWrapper.GetImage(devId, ref unmanagedImagePtr, false, true);

                        //Update Width and Height - Probably not necessary after capture starts. Move to StartCapture!
                        ImageWidth  = VIWrapper.GetWidth(devId);
                        ImageHeight = VIWrapper.GetHeight(devId);

                        int bytesPerPixel = 3;     //NOTE:Assumption of RGB24 starts here!

                        lock (bufferGuard)         /* Enter critical section to copy data into buffer */
                        {
                            if (unmanagedImagePtr != IntPtr.Zero)
                            {
                                unsafe
                                {
                                    Marshal.Copy(unmanagedImagePtr, buffer1, 0, (imageWidth * imageHeight * bytesPerPixel));
                                }
                            }
                        }

                        //      Marshal.FreeHGlobal(unmanagedImagePtr); //Free the data since it should be managed.
                        GC.Collect();         //Enforced GC because 60frames passes a TON of data

                        //Now, call back registered members!
                        //Maybe Lock each call?? If i get crashes, try that.
                        foreach (Func <byte[], int> function in _callbackMethods)
                        {
                            Application.Current.Dispatcher.BeginInvoke(
                                DispatcherPriority.Loaded,
                                new Action(() =>
                            {
                                try
                                {
                                    function(buffer1);
                                }
                                catch (Exception e)
                                { }
                            }));
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("CAPTURE FAILURE please report to dev!. Exception -->" + e);
                        Console.WriteLine("Exception Source: " + e.Source);
                        Console.WriteLine("Stack Trace:" + e.StackTrace);
                    }
                }
            }
        }