WPF GUI停止响应,因为一些函数在循环中调用

时间:2013-08-27 05:21:43

标签: c# wpf multithreading foreach

点击按钮对用户的脸部和眼睛进行监控后,我的WPF窗口冻结。我正在使用英特尔感知计算SDK。

private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        pipeline = new MyPipeline();
        if (isRunning)
        {
            isRunning = false;
            pipeline.PauseFaceLocation(true);
            pipeline.Close();
        }
        else
        {
            isRunning = true;
            pipeline.LoopFrames();
        }
    }

这个 Util [M] Pipeline 用于逐帧识别脸部。在识别脸部后,我的目的是获得眼睛状态(关闭/打开 )。

    class MyPipeline : UtilMPipeline
    {
        //Core variables
        ulong timeStamp;
        int faceId;
        uint fidx = 0;
        bool eyeClosed = false;
        int ctrlTimerStart = 5, ctrlTimerStop = 3;

        DispatcherTimer screenOffTimer = new DispatcherTimer();
        DispatcherTimer gotoSleepTimer = new DispatcherTimer();

        [DllImport("user32.dll", SetLastError = true)]
        private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);

        const int SC_MONITORPOWER = 0xF170;
        const int WM_SYSCOMMAND = 0x0112;

        const int MONITOR_ON = -1;
        const int MONITOR_OFF = 2;
        const int MONITOR_STANBY = 1;

        int HWND_BROADCAST = 0xffff;


        //Statuses
        pxcmStatus locationStatus;
        pxcmStatus landmarkStatus;
        pxcmStatus attributeStatus;
        public bool takeRecoSnapshot = false;

        //Form variables
        MainWindow parent;
        Bitmap lastProcessedBitmap;

        //Attribute array                
        uint[] blink = new uint[2];

        //PXCM variables
        PXCMFaceAnalysis faceAnalysis;
        PXCMSession session;
        PXCMFaceAnalysis.Detection faceLocation;
        PXCMFaceAnalysis.Landmark faceLandmark;
        PXCMFaceAnalysis.Attribute faceAttributes;
        PXCMFaceAnalysis.Detection.Data faceLocationData;
        PXCMFaceAnalysis.Landmark.LandmarkData[] faceLandmarkData;
        PXCMFaceAnalysis.Landmark.ProfileInfo landmarkProfile;
        PXCMFaceAnalysis.Attribute.ProfileInfo attributeProfile;

        public MyPipeline()
        {
            lastProcessedBitmap = new Bitmap(640, 480);

            attributeProfile = new PXCMFaceAnalysis.Attribute.ProfileInfo();

            EnableImage(PXCMImage.ColorFormat.COLOR_FORMAT_RGB24);
            EnableFaceLocation();
            EnableFaceLandmark();
        }


        public override bool OnNewFrame()
        {
            faceAnalysis = QueryFace();
            faceAnalysis.QueryFace(fidx, out faceId, out timeStamp);

            //Get face location
            faceLocation = (PXCMFaceAnalysis.Detection)faceAnalysis.DynamicCast(PXCMFaceAnalysis.Detection.CUID);
            locationStatus = faceLocation.QueryData(faceId, out faceLocationData);

            //Get face landmarks (eye, mouth, nose position, etc)
            faceLandmark = (PXCMFaceAnalysis.Landmark)faceAnalysis.DynamicCast(PXCMFaceAnalysis.Landmark.CUID);
            faceLandmark.QueryProfile(1, out landmarkProfile);
            faceLandmark.SetProfile(ref landmarkProfile);
            faceLandmarkData = new PXCMFaceAnalysis.Landmark.LandmarkData[7];
            landmarkStatus = faceLandmark.QueryLandmarkData(faceId, PXCMFaceAnalysis.Landmark.Label.LABEL_7POINTS, faceLandmarkData);

            //Get face attributes (eye blink)
            faceAttributes = (PXCMFaceAnalysis.Attribute)faceAnalysis.DynamicCast(PXCMFaceAnalysis.Attribute.CUID);
            faceAttributes.QueryProfile(PXCMFaceAnalysis.Attribute.Label.LABEL_EYE_CLOSED, 0, out attributeProfile);
            attributeProfile.threshold = 50; //Must be here!
            faceAttributes.SetProfile(PXCMFaceAnalysis.Attribute.Label.LABEL_EYE_CLOSED, ref attributeProfile);
            attributeStatus = faceAttributes.QueryData(PXCMFaceAnalysis.Attribute.Label.LABEL_EYE_CLOSED, faceId, out blink);


            ComputationOfTimer();
            return true;
        }

并且每个帧都会调用 ComputationOfTimer(); 函数,当眼睛关闭一定时间后启动计时器。

private void ComputationOfTimer()
        {
            if (blink[0] == 100)        //If eye Closed detected
            {
                ctrlTimerStop = 3;
                ctrlTimerStart = ctrlTimerStart - 1;
                System.Console.Write("\n\t Eyes Closed");                    
                timerStarting();
            }
            else                        //If eyes are open we have to stop timer
            {
                ctrlTimerStart = 5;
                ctrlTimerStop -= 1;
                System.Console.Write("\n\t\t\t\t\t Opened");
                timerStopping();
            }
        }

        public void timerStarting()
        {
            if (ctrlTimerStart <= 0)
            {

                screenOffTimer.Interval = new TimeSpan(0, 0, 5);
                screenOffTimer.Tick += screenOffTimer_Tick_ScreenOff;
                screenOffTimer.Start();
                System.Console.Write("Timer is running");
                SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);
            }
        }

        void screenOffTimer_Tick_ScreenOff(object sender, EventArgs e)
        {
            System.Console.Write("Eyes Closed For long time!");
            SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);


            //gotoSleepTimer.Interval = new TimeSpan(0, 0, 20);
            //gotoSleepTimer.Tick += gotoSleepTimer_Tick_SleepOff;
            //gotoSleepTimer.Start();

        }

        //void gotoSleepTimer_Tick_SleepOff(object sender, EventArgs e) 
        //{
        //    System.Windows.Forms.Application.SetSuspendState(PowerState.Suspend,false,false);
        //}

        public void timerStopping()
        {
            if (ctrlTimerStop <= 0)
            {
                //timer stop logic
                screenOffTimer.Stop();
                SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON);
                System.Console.Write("Timer stopped");
            }
        }
    }

此功能正常,但我的WPF窗口停止响应。 那么请你告诉我如何在循环中调用函数的情况下使用线程。

这是Window的截图: WPF Window and the button that generate event is "Click Me"

1 个答案:

答案 0 :(得分:1)

您可以使用线程或使用异步,但很难判断您是否正在触摸代码中的任何UI

主题:

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        ThreadPool.QueueUserWorkItem((o) =>
            {
                pipeline = new MyPipeline();
                if (isRunning)
                {
                    isRunning = false;
                    pipeline.PauseFaceLocation(true);
                    pipeline.Close();
                }
                else
                {
                    isRunning = true;
                    pipeline.LoopFrames();
                }
            });
    }

异步

    private async void Button_Click_1(object sender, RoutedEventArgs e)
    {
       await Task.Factory.StartNew(() =>
            {
                pipeline = new MyPipeline();
                if (isRunning)
                {
                    isRunning = false;
                    pipeline.PauseFaceLocation(true);
                    pipeline.Close();
                }
                else
                {
                    isRunning = true;
                    pipeline.LoopFrames();
                }
            });
    }