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);
            }
        }
    /// <summary>
    /// <para>Executes functions specified in the constructor.</para>
    /// <para>e.g. Face detection, Age estimation etc.</para>
    /// </summary>
    /// <param name="out_img_type">output image type
    /// <para>    OUT_IMG_TYPE_NONE  (00h): no image output</para>
    /// <para>    OUT_IMG_TYPE_QVGA  (01h): 320x240 pixel resolution(QVGA)</para>
    /// <para>         OUT_IMG_TYPE_QQVGA (02h): 160x120 pixel resolution(QQVGA)</para></param>
    /// <param name="tracking_result">the tracking result is stored</param>
    /// <param name="out_img">output image</param>
    /// <returns>struct of (response_code, stb_return)
    /// <para>    response_code (int): response code form B5T-007001</para>
    /// <para>    stb_return (bool): return status of STB library</para>
    /// </returns>
    public EXECUTE_RET execute(int out_img_type, HVCTrackingResult tracking_result, GrayscaleImage out_img)
    {
        EXECUTE_RET retvalue;

        retvalue.response_code = 0;
        retvalue.stb_return    = 0;

        var frame_result = new HVCResult();

        retvalue.response_code = this._hvc_p2_wrapper.execute(this._exec_func, out_img_type, frame_result, out_img);
        tracking_result.clear();

        if ((this.use_stb == true) && (this._exec_func != p2def.EX_NONE))
        {
            var stb_in = new STBLibWrapper.STB_FRAME_RESULT();

            stb_in.bodys.body = new STBLibWrapper.STB_FRAME_RESULT_DETECTION[STB.STB_FRAME_NUM];
            stb_in.faces.face = new STBLibWrapper.STB_FRAME_RESULT_FACE[STB.STB_FRAME_NUM];

            frame_result.export_to_C_FRAME_RESULT(ref stb_in);

            var stb_out_f = new STBLibWrapper.STB_FACE[STB.STB_FRAME_NUM];
            var stb_out_b = new STBLibWrapper.STB_BODY[STB.STB_FRAME_NUM];

            STB.STB_EXECUTE_RET stb_ret = this._stb.execute(stb_in, stb_out_f, stb_out_b);

            if (stb_ret.retcode < 0)
            {
                // STB error
                retvalue.stb_return = stb_ret.retcode;

                return(retvalue);
            }

            tracking_result.faces.append_C_FACE_RES35(this._exec_func, (int)stb_ret.face_count, stb_out_f);

            if ((this._exec_func & p2def.EX_DIRECTION) != 0)
            {
                tracking_result.faces.append_direction_list(frame_result.faces);
            }

            if ((this._exec_func & p2def.EX_GAZE) != 0)
            {
                tracking_result.faces.append_gaze_list(frame_result.faces);
            }

            if ((this._exec_func & p2def.EX_BLINK) != 0)
            {
                tracking_result.faces.append_blink_list(frame_result.faces);
            }

            if ((this._exec_func & p2def.EX_EXPRESSION) != 0)
            {
                tracking_result.faces.append_expression_list(frame_result.faces);
            }

            tracking_result.bodies.append_BODY_RES35(this._exec_func, (int)stb_ret.body_count, stb_out_b);
            tracking_result.hands.append_hand_list(frame_result.hands);
        }
        else
        {
            tracking_result.appned_FRAME_RESULT(frame_result);
        }
        return(retvalue);
    }