private void MainWindow_Load(object sender, EventArgs e)
        {
            // instance initialization requires UI thread, wait until load
            this.model = GestureModel.Instance;
            this.gestures = Gestures.Instance;
            this.keyControls = KeyControls.Instance;
            this.controller = new Control();
            this.LoadTable();
            this.LoadTutorial();
            this.volumeTimer = new Stopwatch();
            this.volumeTimer.Start();
            this.bWork = new BackgroundWorker();
            bWork.DoWork += new DoWorkEventHandler(bw_DoWork);

            // direct to tutorial page if necessary
            string[] lines = File.ReadAllLines(GestureStudio.SettingFile);
            string[] directTutorial = lines[0].Split(':');
            if (directTutorial[0] == "directTutorial" && directTutorial[1] == "yes")
            {
                using (DirectToTutorialForm directForm = new DirectToTutorialForm())
                {
                    DialogResult result = directForm.ShowDialog();
                    if (DialogResult.Yes == result)
                    {
                        this.mainWindowTabs.SelectedTab = this.tutorialTab;
                    }
                    else if (DialogResult.No == result)
                    {
                        // don't show this dialog next time
                        if (directForm.isIgnoreChecked())
                        {
                            using (StreamWriter file = new StreamWriter(GestureStudio.SettingFile))
                            {
                                StringBuilder sb = new StringBuilder();
                                sb.Append(directTutorial[0] + ":no");
                                file.WriteLine(sb.ToString());
                            }
                        }
                    }
                    else
                    {
                    }

                }
            }

            SynchronizationContext ctx = SynchronizationContext.Current;


            this.model.FrameReady += (s, args) =>
            {
                if (disabled)
                {
                    return;
                }
                
                Bitmap fullFrame = this.model.RawDepthFrame.ToBitmap();
                Bitmap croppedFrame = this.model.CroppedFrame.ToBitmap();

                using (Graphics g = Graphics.FromImage(fullFrame))
                {
                    int startX = this.model.CropStartX;
                    int startY = this.model.CropStartY;
                    int croppedWidth = this.model.CroppedFrame.Width;
                    int croppedHeight = this.model.CroppedFrame.Height;
                    Pen pen = new System.Drawing.Pen(System.Drawing.Color.Red, 5);
                    if (croppedWidth > 10 && croppedHeight > 10 && croppedHeight < 200 && croppedHeight < 200) {
                        g.DrawRectangle(pen, startX + croppedWidth / 2, startY + croppedHeight / 2, 1, 1);
                        g.DrawRectangle(pen, startX, startY, croppedWidth, croppedHeight);
                    }
                }
                ctx.Post((o) =>
                {
                    Bitmap fitFull = new Bitmap(fullFrame, this.mainWindow_full.Width, this.mainWindow_full.Height);
                    Bitmap fitCropped;
                    if (GestureStudio.DISPLAY_DETECTED_GESTURE_IMG)
                    {


                    }
                    else
                    {
                        // make sure the cropped image has area
                        if (croppedFrame.Height > 0 && croppedFrame.Width > 0)
                        {
                            // resize images in order to fit into picture box in the home tab
                            double croppedRatio_w_h = (double)croppedFrame.Width / croppedFrame.Height;
                            if (croppedRatio_w_h > Width_To_Height_Ratio)  // cropped image is long in horizontal
                            {
                                fitCropped = new Bitmap(croppedFrame, this.mainWindow_cropped.Width, (int)(this.mainWindow_cropped.Width / croppedRatio_w_h));
                            }
                            else  // cropped image is long in vertical
                            {
                                fitCropped = new Bitmap(croppedFrame, (int)(this.mainWindow_cropped.Height * croppedRatio_w_h), this.mainWindow_cropped.Height);
                            }
                        }
                        else
                            fitCropped = null;

                        this.mainWindow_cropped.Image = fitCropped;
                    }




                    this.mainWindow_full.Image = fitFull;

                    framesCount++;
                }, null);

            };

            this.model.CategoryDetected += (s, args) =>
            {
                if (disabled)
                {
                    return;
                }

                ctx.Post((o) =>
                {
                    int label = (int)o;
                    if (GestureStudio.GENERIC_GESTURES)
                    {


                        if (this.volumeTimer.ElapsedMilliseconds > 2500)
                        {
                            
                            
                            if (GestureStudio.DISPLAY_DETECTED_GESTURE_IMG) {
                                string img_path = GestureStudio.GestureImagePath + "/" + Gestures.getGestureName(label) + ".png";

                                Bitmap resized_img = null;
                                if (File.Exists(img_path))
                                {
                                    Bitmap img = new Bitmap(img_path);
                                    // resize

                                    resized_img = new Bitmap(img, this.mainWindow_cropped.Width, this.mainWindow_cropped.Height);
                                }

                                this.mainWindow_cropped.Image = resized_img;
                            }
                            // lookup which window is focused and find if it is in the gestures list
                            if (Gestures.getGestureName(label) != null && Gestures.getGestureName(label).ToLower() != "noise")
                            {

                                this.mainWindow_status.Text = "Your Gesture: [" + Gestures.getGestureName(label) + "]";
                                if (Gestures.getGestures()[label].getAllCommands().Count != 0)
                                    this.commandLabel.Text = "[" + Gestures.getGestures()[label].getAllCommands()[0].getCommand() + "]";
                                else
                                    this.commandLabel.Text = "[]";
                            }
                            else
                            {
                                this.mainWindow_status.Text = "Your Gesture: []";
                                this.commandLabel.Text = "[]";
                            }
                            // string focusedApp = ...
                            // int appId = Gestures.getAppId(focusedApp);
                            AppKeyInfo appInfo = Gestures.getAppKeyForGesture(label, 0 /*appId*/);
                            if (appInfo == null || KeyControls.getKeyMatches()[0/*appId*/] == null)
                                return;

                            string detectedCommand = KeyControls.getKeyMatches()[0][appInfo.getCommand()];
                            if (detectedCommand != null && !detectedCommand.Equals("f8") && !detectedCommand.Equals("f9"))
                            {
                                this.volumeTimer.Reset();
                                this.volumeTimer.Start();
                            }

                            if (!this.bWork.IsBusy) {
                                this.bWork.RunWorkerAsync(detectedCommand); //() => { this.controller.parseThenExecute(detectedCommand);}); 
                            }
                            
                        }                    
                        
                    } 
                    else
                        this.mainWindow_status.Text = "Your Gesture: [" + LabelToString(label) + "]";
                }, args.CategoryLabel);
            };


        }
        private void MainWindow_Load(object sender, EventArgs e)
        {
            // instance initialization requires UI thread, wait until load
            this.model       = GestureModel.Instance;
            this.gestures    = Gestures.Instance;
            this.keyControls = KeyControls.Instance;
            this.controller  = new Control();
            this.LoadTable();
            this.LoadTutorial();
            this.volumeTimer = new Stopwatch();
            this.volumeTimer.Start();
            this.bWork    = new BackgroundWorker();
            bWork.DoWork += new DoWorkEventHandler(bw_DoWork);

            // direct to tutorial page if necessary
            string[] lines          = File.ReadAllLines(GestureStudio.SettingFile);
            string[] directTutorial = lines[0].Split(':');
            if (directTutorial[0] == "directTutorial" && directTutorial[1] == "yes")
            {
                using (DirectToTutorialForm directForm = new DirectToTutorialForm())
                {
                    DialogResult result = directForm.ShowDialog();
                    if (DialogResult.Yes == result)
                    {
                        this.mainWindowTabs.SelectedTab = this.tutorialTab;
                    }
                    else if (DialogResult.No == result)
                    {
                        // don't show this dialog next time
                        if (directForm.isIgnoreChecked())
                        {
                            using (StreamWriter file = new StreamWriter(GestureStudio.SettingFile))
                            {
                                StringBuilder sb = new StringBuilder();
                                sb.Append(directTutorial[0] + ":no");
                                file.WriteLine(sb.ToString());
                            }
                        }
                    }
                    else
                    {
                    }
                }
            }

            SynchronizationContext ctx = SynchronizationContext.Current;


            this.model.FrameReady += (s, args) =>
            {
                if (disabled)
                {
                    return;
                }

                Bitmap fullFrame    = this.model.RawDepthFrame.ToBitmap();
                Bitmap croppedFrame = this.model.CroppedFrame.ToBitmap();

                using (Graphics g = Graphics.FromImage(fullFrame))
                {
                    int startX        = this.model.CropStartX;
                    int startY        = this.model.CropStartY;
                    int croppedWidth  = this.model.CroppedFrame.Width;
                    int croppedHeight = this.model.CroppedFrame.Height;
                    Pen pen           = new System.Drawing.Pen(System.Drawing.Color.Red, 5);
                    if (croppedWidth > 10 && croppedHeight > 10 && croppedHeight < 200 && croppedHeight < 200)
                    {
                        g.DrawRectangle(pen, startX + croppedWidth / 2, startY + croppedHeight / 2, 1, 1);
                        g.DrawRectangle(pen, startX, startY, croppedWidth, croppedHeight);
                    }
                }
                ctx.Post((o) =>
                {
                    Bitmap fitFull = new Bitmap(fullFrame, this.mainWindow_full.Width, this.mainWindow_full.Height);
                    Bitmap fitCropped;
                    if (GestureStudio.DISPLAY_DETECTED_GESTURE_IMG)
                    {
                    }
                    else
                    {
                        // make sure the cropped image has area
                        if (croppedFrame.Height > 0 && croppedFrame.Width > 0)
                        {
                            // resize images in order to fit into picture box in the home tab
                            double croppedRatio_w_h = (double)croppedFrame.Width / croppedFrame.Height;
                            if (croppedRatio_w_h > Width_To_Height_Ratio)  // cropped image is long in horizontal
                            {
                                fitCropped = new Bitmap(croppedFrame, this.mainWindow_cropped.Width, (int)(this.mainWindow_cropped.Width / croppedRatio_w_h));
                            }
                            else  // cropped image is long in vertical
                            {
                                fitCropped = new Bitmap(croppedFrame, (int)(this.mainWindow_cropped.Height * croppedRatio_w_h), this.mainWindow_cropped.Height);
                            }
                        }
                        else
                        {
                            fitCropped = null;
                        }

                        this.mainWindow_cropped.Image = fitCropped;
                    }



                    this.mainWindow_full.Image = fitFull;

                    framesCount++;
                }, null);
            };

            this.model.CategoryDetected += (s, args) =>
            {
                if (disabled)
                {
                    return;
                }

                ctx.Post((o) =>
                {
                    int label = (int)o;
                    if (GestureStudio.GENERIC_GESTURES)
                    {
                        if (this.volumeTimer.ElapsedMilliseconds > 2500)
                        {
                            if (GestureStudio.DISPLAY_DETECTED_GESTURE_IMG)
                            {
                                string img_path = GestureStudio.GestureImagePath + "/" + Gestures.getGestureName(label) + ".png";

                                Bitmap resized_img = null;
                                if (File.Exists(img_path))
                                {
                                    Bitmap img = new Bitmap(img_path);
                                    // resize

                                    resized_img = new Bitmap(img, this.mainWindow_cropped.Width, this.mainWindow_cropped.Height);
                                }

                                this.mainWindow_cropped.Image = resized_img;
                            }
                            // lookup which window is focused and find if it is in the gestures list
                            if (Gestures.getGestureName(label) != null && Gestures.getGestureName(label).ToLower() != "noise")
                            {
                                this.mainWindow_status.Text = "Your Gesture: [" + Gestures.getGestureName(label) + "]";
                                if (Gestures.getGestures()[label].getAllCommands().Count != 0)
                                {
                                    this.commandLabel.Text = "[" + Gestures.getGestures()[label].getAllCommands()[0].getCommand() + "]";
                                }
                                else
                                {
                                    this.commandLabel.Text = "[]";
                                }
                            }
                            else
                            {
                                this.mainWindow_status.Text = "Your Gesture: []";
                                this.commandLabel.Text      = "[]";
                            }
                            // string focusedApp = ...
                            // int appId = Gestures.getAppId(focusedApp);
                            AppKeyInfo appInfo = Gestures.getAppKeyForGesture(label, 0 /*appId*/);
                            if (appInfo == null || KeyControls.getKeyMatches()[0 /*appId*/] == null)
                            {
                                return;
                            }

                            string detectedCommand = KeyControls.getKeyMatches()[0][appInfo.getCommand()];
                            if (detectedCommand != null && !detectedCommand.Equals("f8") && !detectedCommand.Equals("f9"))
                            {
                                this.volumeTimer.Reset();
                                this.volumeTimer.Start();
                            }

                            if (!this.bWork.IsBusy)
                            {
                                this.bWork.RunWorkerAsync(detectedCommand); //() => { this.controller.parseThenExecute(detectedCommand);});
                            }
                        }
                    }
                    else
                    {
                        this.mainWindow_status.Text = "Your Gesture: [" + LabelToString(label) + "]";
                    }
                }, args.CategoryLabel);
            };
        }