public void _regist_exec(HVCP2Api hvc_p2_api)
        {
            var out_register_image = new GrayscaleImage();

            var ret = hvc_p2_api.register_data(0, 0, out_register_image);

            if (ret == p2def.RESPONSE_CODE_NORMAL)
            {
                this.SetText("  Success to register. user_id=0 data_id=0");
                this.SetText(Environment.NewLine);
                out_register_image.save("registerd_img.jpg");
            }
            else if (ret == p2def.RESPONSE_CODE_NO_FACE)
            {
                this.SetText("  Number of faces that can be registered is 0.");
                this.SetText(Environment.NewLine);
            }
            else if (ret == p2def.RESPONSE_CODE_PLURAL_FACE)
            {
                this.SetText("  Number of detected faces is 2 or more.");
                this.SetText(Environment.NewLine);
            }
            else
            {
                // error
                this.SetText("  Error: Invalid register album. ");
                this.SetText(Environment.NewLine);
            }
        }
        public void mainloop()
        {
            var hvc_tracking_result = new HVCTrackingResult();
            var img = new GrayscaleImage();

            HVCP2Api.EXECUTE_RET exec_ret;
            var sw = new Stopwatch();

            while (true)
            {
                try
                {
                    if (this.isConnectRequest)
                    {
                        var connector = new SerialConnector();

                        var exec_func = 0x00;

                        if (BodyDetectionCheck.Checked == true)
                        {
                            exec_func += p2def.EX_BODY;
                        }
                        if (HandDetectionCheck.Checked == true)
                        {
                            exec_func += p2def.EX_HAND;
                        }
                        if (FaceDetectionCheck.Checked == true)
                        {
                            exec_func += p2def.EX_FACE;
                        }
                        if (FaceDirectionCheck.Checked == true)
                        {
                            exec_func += p2def.EX_DIRECTION;
                        }
                        if (AgeDetectionCheck.Checked == true)
                        {
                            exec_func += p2def.EX_AGE;
                        }
                        if (GenderDetectionCheck.Checked == true)
                        {
                            exec_func += p2def.EX_GENDER;
                        }
                        if (GazeDetectionCheck.Checked == true)
                        {
                            exec_func += p2def.EX_GAZE;
                        }
                        if (BlinkDetectionCheck.Checked == true)
                        {
                            exec_func += p2def.EX_BLINK;
                        }
                        if (ExpressionDetectionCheck.Checked == true)
                        {
                            exec_func += p2def.EX_EXPRESSION;
                        }
                        if (RecognitionDetectionCheck.Checked == true)
                        {
                            exec_func += p2def.EX_RECOGNITION;
                        }

                        this.hvc_p2_api = new HVCP2Api(connector, exec_func, StablirizationCheck.Checked);

                        var comnum = int.Parse(this.connect_comport.Substring(3));
                        var ret    = this.hvc_p2_api.connect(comnum, 9600, timeout * 1000);
                        if (ret == true)
                        {
                            HVCP2Wrapper.GET_VERSION_RET result;
                            this.isConnectRequest = false;
                            var retcode = _check_connection(this.hvc_p2_api, out result);
                            if (retcode == true)
                            {
                                _get_hvc_version(result);

                                _set_hvc_p2_parameters(this.hvc_p2_api);

                                // Sets STB library parameters
                                _set_stb_parameters(hvc_p2_api);

                                this.isConnected = true;
                            }
                            else
                            {
                                this.SetToDefault();
                                try
                                {
                                    this.hvc_p2_api.disconnect();
                                }
                                catch
                                {
                                }
                            }
                        }
                    }

                    if (this.isConnected)
                    {
                        if (this.isRegistExecute)
                        {
                            _regist_exec(this.hvc_p2_api);
                            this.SetToDefault();
                            try
                            {
                                this.hvc_p2_api.disconnect();
                            }
                            catch
                            {
                            }
                        }
                        else
                        {
                            this.isExecuting = true;

                            sw.Reset();
                            sw.Start();
                            exec_ret = hvc_p2_api.execute(output_img_type, hvc_tracking_result, img);
                            sw.Stop();
                            this.isExecuting = false;

                            if (output_img_type != p2def.OUT_IMG_TYPE_NONE)
                            {
                                img.save(img_fname);
                            }
                            this.SetText(string.Format("  ==== Elapsed time:{0}[msec] ====", sw.ElapsedMilliseconds));
                            this.SetText(Environment.NewLine);
                            this.SetText(hvc_tracking_result.ToString());
                            this.SetText(Environment.NewLine);
                            this.SetText(string.Format("  Press Stop Button to end:", sw.ElapsedMilliseconds));
                            this.SetText(Environment.NewLine);
                            this.SetText(Environment.NewLine);
                            System.Windows.Forms.Application.DoEvents();
                        }
                    }

                    if (this.isEndRequest)
                    {
                        try
                        {
                            this.hvc_p2_api.disconnect();
                        }
                        catch
                        {
                        }
                        break;
                    }
                }
                catch (Exception ex)
                {
                    this.SetText(string.Format("Unexpected exception : {0}", ex.Message));
                    this.SetText(Environment.NewLine);
                    this.SetToDefault();
                    try
                    {
                        this.hvc_p2_api.disconnect();
                    }
                    catch
                    {
                    }
                }

                System.Threading.Thread.Sleep(10);
            }
        }