private void Tab_Unloaded(object sender, RoutedEventArgs e)
        {
            TabItem  t  = (TabItem)sender;
            ConnShow tc = (ConnShow)t.Content;

            tc.Icc.Close();
            this.tabMap.Remove(t.Header.ToString());
            tc.RStop();
            tc.ClearRFPath();
        }
        private void btn_Clear_Click(object sender, RoutedEventArgs e)
        {
            TabItem o = this.tab_Main.SelectedItem as TabItem;

            if (o != null)
            {
                ConnShow c = o.Content as ConnShow;
                if (c != null)
                {
                    c.txt_Data.Clear();
                }
            }
        }
 private void MetroWindow_Closed(object sender, EventArgs e)
 {
     foreach (var item in tabMap)
     {
         ConnShow tc = (ConnShow)(item.Value.Content);
         tc.Icc.Close();
         tc.RStop();
         tc.ClearRFPath();
     }
     foreach (var i in Directory.GetFiles(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, app.Tmp), "*.tmp", SearchOption.TopDirectoryOnly))
     {
         File.Delete(i);
     }
 }
        private void tab_Main_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            TabItem o = tab_Main.SelectedItem as TabItem;

            if (o != null)
            {
                ConnShow c = o.Content as ConnShow;
                if (c != null)
                {
                    if (c.IsPause)
                    {
                        FlyoutPauseContent   = "PAUSE!";
                        FlyoutPauseAutoClose = false;
                        FlyoutPauseShow      = true;
                    }
                    else
                    {
                        FlyoutPauseShow = false;
                    }
                }
            }
        }
        private void btn_RStart_Click(object sender, RoutedEventArgs e)
        {
            Button  btn = (Button)sender;
            TabItem o   = this.tab_Main.SelectedItem as TabItem;

            if (o != null)
            {
                ConnShow c = o.Content as ConnShow;
                if (c != null)
                {
                    if (c.RStart())
                    {
                        btn.Foreground = new SolidColorBrush(Color.FromRgb(0, 255, 0));
                        Debug.WriteLine("Record start");
                    }
                    else
                    {
                        Debug.WriteLine("Record stop");
                        if (c.RStop())
                        {
                            btn.Foreground = new SolidColorBrush(Color.FromRgb(255, 255, 255));
                            System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
                            sfd.Title  = "Save";
                            sfd.Filter = "(*.txt)|*.txt|(*.*)|*.*";
                            sfd.ShowDialog();
                            if (sfd.FileName != string.Empty)
                            {
                                try
                                {
                                    File.Copy(c.RFPath, sfd.FileName, true);
                                }
                                catch (System.IO.IOException)
                                {
                                    MsgBox mb = new MsgBox("Save", "Access denied: This file is in use.", "OK");
                                    mb.Owner = this;
                                    mb.ShowDialog();
                                    return;
                                }
                                c.ClearRFPath();
                            }
                        }
                        else if (c.RFPath != string.Empty)
                        {
                            MsgBox mb = new MsgBox("Save", "Save last result, or start new recording?", "Save", "Restart");
                            mb.Owner = this;
                            mb.ShowDialog();
                            if (mb.Result)
                            {
                                System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
                                sfd.Title  = "Please save last result";
                                sfd.Filter = "(*.txt)|*.txt|(*.*)|*.*";
                                sfd.ShowDialog();
                                if (sfd.FileName != string.Empty)
                                {
                                    try
                                    {
                                        File.Copy(c.RFPath, sfd.FileName, true);
                                    }
                                    catch (System.IO.IOException)
                                    {
                                        MsgBox mb2 = new MsgBox("Save", "Access denied: This file is in use.", "OK");
                                        mb2.Owner = this;
                                        mb2.ShowDialog();
                                        return;
                                    }
                                    c.ClearRFPath();
                                }
                            }
                            else
                            {
                                Debug.WriteLine("Record restart");
                                c.ClearRFPath();
                                c.RStart();
                                btn.Foreground = new SolidColorBrush(Color.FromRgb(0, 255, 0));
                            }
                        }
                    }
                }
            }
        }