using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Configuration;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms.DataVisualization.Charting;
using System.Diagnostics;
using System.Collections.Generic;
using Core.Mes.ServerFrameWork;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;

namespace Core.Mes.ServerManager
{
    /// <summary>
    /// FrmSeverMain 的摘要说明。
    /// </summary>
    public class FrmSeverMain : System.Windows.Forms.Form
    {
        #region 变量定义
        private IContainer components = null;
        private string[] _args;
        private TabControl tabControl1;
        private TabPage tabPage1;
        private TabPage tabPage2;
        private TabPage tabPage3;
        private SplitContainer splitContainer1;
        private System.Windows.Forms.DataVisualization.Charting.Chart Chart1;
        private System.Windows.Forms.DataVisualization.Charting.Chart Chart2;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.ListBox listBox1;
        private ResDaemon resDaemon;
        private SrvDaemon srvDaemon;
        private ExtSrvDaemon extSrvDaemon;
        private ClsServerMain cMain;
        private TabPage tabPage4;
        private Infragistics.Win.UltraWinGrid.UltraGrid ugServers;
        private DataSet dataSet1;
        private DataTable dataTable1;

        private ServerConfigure _serverConf;

        private bool IsLogging = false;
        #endregion

        public FrmSeverMain(string[] args)
        {
            _args = args;

            if (_args != null)
            {
                foreach (string arg in _args)
                {
                    if (arg.Trim().ToUpper() == "LOG")
                    {
                        IsLogging = true;
                    }
                }
            }

            //
            // Windows 窗体设计器支持所必需的
            //
            InitializeComponent();
            //
            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            //


            this.tabControl1.Controls.Remove(this.tabPage2);
            this.tabControl1.Controls.Remove(this.tabPage3);
            this.tabControl1.Controls.Remove(this.tabPage4);
        }

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing)
                {
                    if (cMain != null)
                    {
                        cMain.Dispose();
                    }
                    if (components != null)
                    {
                        components.Dispose();
                    }
                }


                System.GC.Collect();
                base.Dispose(disposing);
            }
            catch
            {

            }

        }

        #region Windows 窗体设计器生成的代码
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmSeverMain));
            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
            System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
            System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
            System.Windows.Forms.DataVisualization.Charting.Series series2 = new System.Windows.Forms.DataVisualization.Charting.Series();
            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea2 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
            System.Windows.Forms.DataVisualization.Charting.Legend legend2 = new System.Windows.Forms.DataVisualization.Charting.Legend();
            System.Windows.Forms.DataVisualization.Charting.LegendItem legendItem1 = new System.Windows.Forms.DataVisualization.Charting.LegendItem();
            System.Windows.Forms.DataVisualization.Charting.LegendCell legendCell1 = new System.Windows.Forms.DataVisualization.Charting.LegendCell();
            System.Windows.Forms.DataVisualization.Charting.LegendCell legendCell2 = new System.Windows.Forms.DataVisualization.Charting.LegendCell();
            System.Windows.Forms.DataVisualization.Charting.Series series3 = new System.Windows.Forms.DataVisualization.Charting.Series();
            System.Windows.Forms.DataVisualization.Charting.Series series4 = new System.Windows.Forms.DataVisualization.Charting.Series();
            Infragistics.Win.Appearance appearance1 = new Infragistics.Win.Appearance("1", 65116440);
            Infragistics.Win.Appearance appearance2 = new Infragistics.Win.Appearance("2", 65174161);
            Infragistics.Win.Appearance appearance3 = new Infragistics.Win.Appearance("3", 65316418);
            Infragistics.Win.Appearance appearance4 = new Infragistics.Win.Appearance("11", 65318540);
            Infragistics.Win.Appearance appearance5 = new Infragistics.Win.Appearance("12", 65321145);
            Infragistics.Win.Appearance appearance6 = new Infragistics.Win.Appearance("13", 65325154);
            Infragistics.Win.Appearance appearance7 = new Infragistics.Win.Appearance("24", 65327806);
            Infragistics.Win.Appearance appearance8 = new Infragistics.Win.Appearance("25", 65329756);
            Infragistics.Win.Appearance appearance9 = new Infragistics.Win.Appearance("4", 22929605);
            Infragistics.Win.Appearance appearance10 = new Infragistics.Win.Appearance("14", 23236973);
            Infragistics.Win.Appearance appearance11 = new Infragistics.Win.Appearance();
            Infragistics.Win.Appearance appearance12 = new Infragistics.Win.Appearance();
            Infragistics.Win.Appearance appearance13 = new Infragistics.Win.Appearance();
            Infragistics.Win.Appearance appearance14 = new Infragistics.Win.Appearance();
            Infragistics.Win.Appearance appearance15 = new Infragistics.Win.Appearance();
            Infragistics.Win.Appearance appearance16 = new Infragistics.Win.Appearance();
            Infragistics.Win.Appearance appearance17 = new Infragistics.Win.Appearance();
            Infragistics.Win.Appearance appearance18 = new Infragistics.Win.Appearance();
            Infragistics.Win.Appearance appearance19 = new Infragistics.Win.Appearance();
            Infragistics.Win.Appearance appearance20 = new Infragistics.Win.Appearance();
            Infragistics.Win.Appearance appearance21 = new Infragistics.Win.Appearance();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.tabControl1 = new System.Windows.Forms.TabControl();
            this.tabPage1 = new System.Windows.Forms.TabPage();
            this.tabPage2 = new System.Windows.Forms.TabPage();
            this.Chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
            this.tabPage3 = new System.Windows.Forms.TabPage();
            this.Chart2 = new System.Windows.Forms.DataVisualization.Charting.Chart();
            this.tabPage4 = new System.Windows.Forms.TabPage();
            this.ugServers = new Infragistics.Win.UltraWinGrid.UltraGrid();
            this.dataSet1 = new System.Data.DataSet();
            this.dataTable1 = new System.Data.DataTable();
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.tabControl1.SuspendLayout();
            this.tabPage1.SuspendLayout();
            this.tabPage2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.Chart1)).BeginInit();
            this.tabPage3.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.Chart2)).BeginInit();
            this.tabPage4.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.ugServers)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataTable1)).BeginInit();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(862, 116);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            // 
            // listBox1
            // 
            this.listBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.listBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.listBox1.ItemHeight = 12;
            this.listBox1.Location = new System.Drawing.Point(3, 3);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(848, 362);
            this.listBox1.TabIndex = 1;
            // 
            // tabControl1
            // 
            this.tabControl1.Controls.Add(this.tabPage1);
            this.tabControl1.Controls.Add(this.tabPage2);
            this.tabControl1.Controls.Add(this.tabPage3);
            this.tabControl1.Controls.Add(this.tabPage4);
            this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tabControl1.Location = new System.Drawing.Point(0, 0);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(862, 396);
            this.tabControl1.TabIndex = 2;
            this.tabControl1.Selected += new System.Windows.Forms.TabControlEventHandler(this.tabControl1_Selected);
            // 
            // tabPage1
            // 
            this.tabPage1.Controls.Add(this.listBox1);
            this.tabPage1.Location = new System.Drawing.Point(4, 22);
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage1.Size = new System.Drawing.Size(854, 370);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.Text = "消息列表";
            this.tabPage1.UseVisualStyleBackColor = true;
            // 
            // tabPage2
            // 
            this.tabPage2.AutoScroll = true;
            this.tabPage2.Controls.Add(this.Chart1);
            this.tabPage2.Location = new System.Drawing.Point(4, 22);
            this.tabPage2.Name = "tabPage2";
            this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage2.Size = new System.Drawing.Size(854, 370);
            this.tabPage2.TabIndex = 1;
            this.tabPage2.Text = "服务使用";
            this.tabPage2.UseVisualStyleBackColor = true;
            // 
            // Chart1
            // 
            chartArea1.AxisX.InterlacedColor = System.Drawing.Color.WhiteSmoke;
            chartArea1.AxisX.IsInterlaced = true;
            chartArea1.AxisX.IsLabelAutoFit = false;
            chartArea1.AxisX.IsMarksNextToAxis = false;
            chartArea1.AxisX.LineColor = System.Drawing.Color.Fuchsia;
            chartArea1.AxisX.LineWidth = 2;
            chartArea1.AxisX.MajorGrid.Enabled = false;
            chartArea1.AxisX.MajorGrid.Interval = 0;
            chartArea1.AxisX.MajorTickMark.Enabled = false;
            chartArea1.AxisX.MajorTickMark.Interval = 1;
            chartArea1.AxisX.MajorTickMark.TickMarkStyle = System.Windows.Forms.DataVisualization.Charting.TickMarkStyle.None;
            chartArea1.AxisX.ScaleBreakStyle.Spacing = 1;
            chartArea1.AxisX.ScaleView.MinSizeType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Number;
            chartArea1.AxisX.ScaleView.SizeType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Number;
            chartArea1.AxisX.ScaleView.Zoomable = false;
            chartArea1.AxisX.TitleAlignment = System.Drawing.StringAlignment.Near;
            chartArea1.AxisY.Enabled = System.Windows.Forms.DataVisualization.Charting.AxisEnabled.True;
            chartArea1.AxisY.InterlacedColor = System.Drawing.Color.FromArgb(((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))));
            chartArea1.AxisY.IsLabelAutoFit = false;
            chartArea1.AxisY.IsMarksNextToAxis = false;
            chartArea1.AxisY.LineColor = System.Drawing.Color.Blue;
            chartArea1.AxisY.LineWidth = 2;
            chartArea1.AxisY.MajorGrid.Enabled = false;
            chartArea1.AxisY.MajorGrid.IntervalOffset = 0;
            chartArea1.AxisY.MajorGrid.LineColor = System.Drawing.Color.Blue;
            chartArea1.AxisY.MajorTickMark.LineColor = System.Drawing.Color.Blue;
            chartArea1.AxisY.MajorTickMark.LineWidth = 2;
            chartArea1.AxisY.MajorTickMark.TickMarkStyle = System.Windows.Forms.DataVisualization.Charting.TickMarkStyle.InsideArea;
            chartArea1.AxisY.Minimum = 0;
            chartArea1.AxisY2.IsMarksNextToAxis = false;
            chartArea1.AxisY2.LabelAutoFitStyle = ((System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles)((((System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.IncreaseFont | System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.DecreaseFont)
                        | System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.StaggeredLabels)
                        | System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.LabelsAngleStep30)));
            chartArea1.AxisY2.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
            chartArea1.AxisY2.LineWidth = 2;
            chartArea1.AxisY2.MajorGrid.Enabled = false;
            chartArea1.AxisY2.MajorTickMark.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
            chartArea1.AxisY2.MajorTickMark.LineWidth = 2;
            chartArea1.AxisY2.MajorTickMark.TickMarkStyle = System.Windows.Forms.DataVisualization.Charting.TickMarkStyle.InsideArea;
            chartArea1.AxisY2.Minimum = 0;
            chartArea1.AxisY2.MinorTickMark.TickMarkStyle = System.Windows.Forms.DataVisualization.Charting.TickMarkStyle.None;
            chartArea1.AxisY2.TitleAlignment = System.Drawing.StringAlignment.Near;
            chartArea1.InnerPlotPosition.Auto = false;
            chartArea1.InnerPlotPosition.Height = 76.16758F;
            chartArea1.InnerPlotPosition.Width = 91.96336F;
            chartArea1.InnerPlotPosition.X = 6.68718F;
            chartArea1.InnerPlotPosition.Y = 5.76923F;
            chartArea1.Name = "ChartArea1";
            this.Chart1.ChartAreas.Add(chartArea1);
            this.Chart1.Dock = System.Windows.Forms.DockStyle.Fill;
            legend1.Name = "Legend1";
            this.Chart1.Legends.Add(legend1);
            this.Chart1.Location = new System.Drawing.Point(3, 3);
            this.Chart1.Margin = new System.Windows.Forms.Padding(0);
            this.Chart1.Name = "Chart1";
            series1.ChartArea = "ChartArea1";
            series1.Legend = "Legend1";
            series1.LegendText = "服务已使用数";
            series1.Name = "Series1";
            series2.BackImageWrapMode = System.Windows.Forms.DataVisualization.Charting.ChartImageWrapMode.TileFlipX;
            series2.BackSecondaryColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(128)))));
            series2.BorderColor = System.Drawing.Color.Red;
            series2.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.NotSet;
            series2.BorderWidth = 0;
            series2.ChartArea = "ChartArea1";
            series2.Color = System.Drawing.Color.DarkOrange;
            series2.EmptyPointStyle.Color = System.Drawing.Color.Red;
            series2.Legend = "Legend1";
            series2.LegendText = "服务实例上限";
            series2.Name = "Series2";
            series2.YAxisType = System.Windows.Forms.DataVisualization.Charting.AxisType.Secondary;
            this.Chart1.Series.Add(series1);
            this.Chart1.Series.Add(series2);
            this.Chart1.Size = new System.Drawing.Size(848, 364);
            this.Chart1.TabIndex = 0;
            // 
            // tabPage3
            // 
            this.tabPage3.Controls.Add(this.Chart2);
            this.tabPage3.Location = new System.Drawing.Point(4, 22);
            this.tabPage3.Name = "tabPage3";
            this.tabPage3.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage3.Size = new System.Drawing.Size(854, 370);
            this.tabPage3.TabIndex = 2;
            this.tabPage3.Text = "资源使用";
            this.tabPage3.UseVisualStyleBackColor = true;
            // 
            // Chart2
            // 
            chartArea2.AxisX.IsMarginVisible = false;
            chartArea2.AxisX.IsStartedFromZero = false;
            chartArea2.AxisX.LineColor = System.Drawing.Color.Empty;
            chartArea2.AxisX.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.NotSet;
            chartArea2.AxisX.LineWidth = 0;
            chartArea2.AxisX.MajorGrid.Enabled = false;
            chartArea2.AxisX.MajorGrid.Interval = 0;
            chartArea2.AxisX.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;
            chartArea2.AxisX.ScaleBreakStyle.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.NotSet;
            chartArea2.AxisY.LineColor = System.Drawing.Color.Red;
            chartArea2.AxisY.MajorGrid.Enabled = false;
            chartArea2.AxisY.MajorGrid.LineColor = System.Drawing.Color.DarkOrange;
            chartArea2.AxisY.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;
            chartArea2.AxisY.MajorTickMark.TickMarkStyle = System.Windows.Forms.DataVisualization.Charting.TickMarkStyle.InsideArea;
            chartArea2.AxisY2.LineColor = System.Drawing.Color.DodgerBlue;
            chartArea2.AxisY2.MajorGrid.LineColor = System.Drawing.Color.DodgerBlue;
            chartArea2.AxisY2.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;
            chartArea2.AxisY2.MajorTickMark.TickMarkStyle = System.Windows.Forms.DataVisualization.Charting.TickMarkStyle.InsideArea;
            chartArea2.BorderWidth = 2;
            chartArea2.Name = "ChartArea1";
            this.Chart2.ChartAreas.Add(chartArea2);
            this.Chart2.Dock = System.Windows.Forms.DockStyle.Fill;
            legend2.BorderWidth = 0;
            legendCell1.ForeColor = System.Drawing.Color.OrangeRed;
            legendCell1.Name = "cell_Content";
            legendCell1.Text = "0";
            legendCell2.Name = "Cell_Label";
            legendCell2.Text = "空闲内存(MB)";
            legendItem1.Cells.Add(legendCell1);
            legendItem1.Cells.Add(legendCell2);
            legend2.CustomItems.Add(legendItem1);
            legend2.LegendItemOrder = System.Windows.Forms.DataVisualization.Charting.LegendItemOrder.SameAsSeriesOrder;
            legend2.LegendStyle = System.Windows.Forms.DataVisualization.Charting.LegendStyle.Column;
            legend2.Name = "Legend1";
            legend2.TableStyle = System.Windows.Forms.DataVisualization.Charting.LegendTableStyle.Tall;
            this.Chart2.Legends.Add(legend2);
            this.Chart2.Location = new System.Drawing.Point(3, 3);
            this.Chart2.Margin = new System.Windows.Forms.Padding(0);
            this.Chart2.Name = "Chart2";
            series3.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
            series3.BorderWidth = 2;
            series3.ChartArea = "ChartArea1";
            series3.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
            series3.Color = System.Drawing.Color.OrangeRed;
            series3.CustomProperties = "LineTension=0.8, LabelStyle=Bottom";
            series3.LabelBorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.NotSet;
            series3.LabelBorderWidth = 2;
            series3.LabelForeColor = System.Drawing.Color.Red;
            series3.Legend = "Legend1";
            series3.LegendText = "内存(MB)";
            series3.MarkerBorderColor = System.Drawing.Color.Red;
            series3.MarkerBorderWidth = 2;
            series3.Name = "Series1";
            series3.ShadowColor = System.Drawing.Color.Transparent;
            series3.SmartLabelStyle.AllowOutsidePlotArea = System.Windows.Forms.DataVisualization.Charting.LabelOutsidePlotAreaStyle.No;
            series3.SmartLabelStyle.CalloutLineAnchorCapStyle = System.Windows.Forms.DataVisualization.Charting.LineAnchorCapStyle.None;
            series3.SmartLabelStyle.CalloutStyle = System.Windows.Forms.DataVisualization.Charting.LabelCalloutStyle.None;
            series3.SmartLabelStyle.IsMarkerOverlappingAllowed = true;
            series3.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Time;
            series3.YValuesPerPoint = 2;
            series3.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
            series4.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;
            series4.BorderWidth = 2;
            series4.ChartArea = "ChartArea1";
            series4.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
            series4.Color = System.Drawing.Color.DodgerBlue;
            series4.CustomProperties = "LineTension=0.8, LabelStyle=Top";
            series4.Legend = "Legend1";
            series4.LegendText = "CPU (%)";
            series4.MarkerBorderWidth = 2;
            series4.Name = "Series2";
            series4.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Time;
            series4.YAxisType = System.Windows.Forms.DataVisualization.Charting.AxisType.Secondary;
            series4.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
            this.Chart2.Series.Add(series3);
            this.Chart2.Series.Add(series4);
            this.Chart2.Size = new System.Drawing.Size(848, 364);
            this.Chart2.TabIndex = 0;
            // 
            // tabPage4
            // 
            this.tabPage4.Controls.Add(this.ugServers);
            this.tabPage4.Location = new System.Drawing.Point(4, 22);
            this.tabPage4.Name = "tabPage4";
            this.tabPage4.Padding = new System.Windows.Forms.Padding(3);
            this.tabPage4.Size = new System.Drawing.Size(854, 370);
            this.tabPage4.TabIndex = 3;
            this.tabPage4.Text = "扩展服务器";
            this.tabPage4.UseVisualStyleBackColor = true;
            // 
            // ugServers
            // 
            this.ugServers.DataMember = "Table1";
            this.ugServers.DataSource = this.dataSet1;
            appearance1.BackColor = System.Drawing.Color.PaleGreen;
            appearance1.BackColor2 = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(0)))));
            appearance1.BackGradientStyle = Infragistics.Win.GradientStyle.VerticalBump;
            appearance1.TextHAlign = Infragistics.Win.HAlign.Center;
            appearance2.BackColor = System.Drawing.Color.Silver;
            appearance2.BackColor2 = System.Drawing.Color.Gray;
            appearance2.BackHatchStyle = Infragistics.Win.BackHatchStyle.BackwardDiagonal;
            appearance2.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(128)))));
            appearance2.TextHAlign = Infragistics.Win.HAlign.Center;
            appearance3.BackColor = System.Drawing.Color.Silver;
            appearance3.BackColor2 = System.Drawing.Color.Gray;
            appearance3.BackGradientStyle = Infragistics.Win.GradientStyle.Vertical;
            appearance3.TextHAlign = Infragistics.Win.HAlign.Center;
            appearance4.BackColor = System.Drawing.Color.PaleGreen;
            appearance4.BackColor2 = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(0)))));
            appearance4.BackGradientStyle = Infragistics.Win.GradientStyle.VerticalBump;
            appearance4.TextHAlign = Infragistics.Win.HAlign.Center;
            appearance5.BackColor = System.Drawing.Color.Silver;
            appearance5.BackColor2 = System.Drawing.Color.Gray;
            appearance5.BackHatchStyle = Infragistics.Win.BackHatchStyle.BackwardDiagonal;
            appearance5.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(128)))));
            appearance5.TextHAlign = Infragistics.Win.HAlign.Center;
            appearance6.BackColor = System.Drawing.Color.Silver;
            appearance6.BackColor2 = System.Drawing.Color.Gray;
            appearance6.BackGradientStyle = Infragistics.Win.GradientStyle.Vertical;
            appearance6.TextHAlign = Infragistics.Win.HAlign.Center;
            appearance7.BackColor = System.Drawing.Color.Brown;
            appearance7.BackColor2 = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
            appearance7.BackGradientStyle = Infragistics.Win.GradientStyle.VerticalBump;
            appearance7.ForeColor = System.Drawing.Color.Lime;
            appearance7.TextHAlign = Infragistics.Win.HAlign.Center;
            appearance8.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(108)))), ((int)(((byte)(108)))), ((int)(((byte)(108)))));
            appearance8.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
            appearance8.TextHAlign = Infragistics.Win.HAlign.Center;
            appearance9.BackColor = System.Drawing.Color.LemonChiffon;
            appearance9.BackColor2 = System.Drawing.Color.Gold;
            appearance9.BackGradientStyle = Infragistics.Win.GradientStyle.VerticalBump;
            appearance9.ForeColor = System.Drawing.Color.Blue;
            appearance9.TextHAlign = Infragistics.Win.HAlign.Center;
            appearance10.BackColor = System.Drawing.Color.LemonChiffon;
            appearance10.BackColor2 = System.Drawing.Color.Gold;
            appearance10.BackGradientStyle = Infragistics.Win.GradientStyle.VerticalBump;
            appearance10.ForeColor = System.Drawing.Color.Blue;
            appearance10.TextHAlign = Infragistics.Win.HAlign.Center;
            this.ugServers.DisplayLayout.Appearances.Add(appearance1);
            this.ugServers.DisplayLayout.Appearances.Add(appearance2);
            this.ugServers.DisplayLayout.Appearances.Add(appearance3);
            this.ugServers.DisplayLayout.Appearances.Add(appearance4);
            this.ugServers.DisplayLayout.Appearances.Add(appearance5);
            this.ugServers.DisplayLayout.Appearances.Add(appearance6);
            this.ugServers.DisplayLayout.Appearances.Add(appearance7);
            this.ugServers.DisplayLayout.Appearances.Add(appearance8);
            this.ugServers.DisplayLayout.Appearances.Add(appearance9);
            this.ugServers.DisplayLayout.Appearances.Add(appearance10);
            this.ugServers.DisplayLayout.BorderStyle = Infragistics.Win.UIElementBorderStyle.Solid;
            this.ugServers.DisplayLayout.BorderStyleCaption = Infragistics.Win.UIElementBorderStyle.None;
            appearance11.BackColor = System.Drawing.SystemColors.ActiveBorder;
            appearance11.BackColor2 = System.Drawing.SystemColors.ControlDark;
            appearance11.BackGradientStyle = Infragistics.Win.GradientStyle.Vertical;
            appearance11.BorderColor = System.Drawing.SystemColors.Window;
            this.ugServers.DisplayLayout.GroupByBox.Appearance = appearance11;
            appearance12.ForeColor = System.Drawing.SystemColors.GrayText;
            this.ugServers.DisplayLayout.GroupByBox.BandLabelAppearance = appearance12;
            this.ugServers.DisplayLayout.GroupByBox.BorderStyle = Infragistics.Win.UIElementBorderStyle.Solid;
            appearance13.BackColor = System.Drawing.SystemColors.ControlLightLight;
            appearance13.BackColor2 = System.Drawing.SystemColors.Control;
            appearance13.BackGradientStyle = Infragistics.Win.GradientStyle.Horizontal;
            appearance13.ForeColor = System.Drawing.SystemColors.GrayText;
            this.ugServers.DisplayLayout.GroupByBox.PromptAppearance = appearance13;
            this.ugServers.DisplayLayout.MaxColScrollRegions = 1;
            this.ugServers.DisplayLayout.MaxRowScrollRegions = 1;
            appearance14.BackColor = System.Drawing.SystemColors.Window;
            appearance14.ForeColor = System.Drawing.SystemColors.ControlText;
            this.ugServers.DisplayLayout.Override.ActiveCellAppearance = appearance14;
            appearance15.BackColor = System.Drawing.SystemColors.Highlight;
            appearance15.ForeColor = System.Drawing.SystemColors.HighlightText;
            this.ugServers.DisplayLayout.Override.ActiveRowAppearance = appearance15;
            this.ugServers.DisplayLayout.Override.AllowAddNew = Infragistics.Win.UltraWinGrid.AllowAddNew.No;
            this.ugServers.DisplayLayout.Override.AllowDelete = Infragistics.Win.DefaultableBoolean.False;
            this.ugServers.DisplayLayout.Override.AllowGroupBy = Infragistics.Win.DefaultableBoolean.False;
            this.ugServers.DisplayLayout.Override.AllowGroupMoving = Infragistics.Win.UltraWinGrid.AllowGroupMoving.NotAllowed;
            this.ugServers.DisplayLayout.Override.AllowGroupSwapping = Infragistics.Win.UltraWinGrid.AllowGroupSwapping.NotAllowed;
            this.ugServers.DisplayLayout.Override.AllowRowFiltering = Infragistics.Win.DefaultableBoolean.True;
            this.ugServers.DisplayLayout.Override.AllowRowLayoutCellSpanSizing = Infragistics.Win.Layout.GridBagLayoutAllowSpanSizing.None;
            this.ugServers.DisplayLayout.Override.AllowRowLayoutLabelSpanSizing = Infragistics.Win.Layout.GridBagLayoutAllowSpanSizing.None;
            this.ugServers.DisplayLayout.Override.AllowUpdate = Infragistics.Win.DefaultableBoolean.False;
            appearance16.BackColor = System.Drawing.SystemColors.Window;
            this.ugServers.DisplayLayout.Override.CardAreaAppearance = appearance16;
            appearance17.BorderColor = System.Drawing.Color.Silver;
            appearance17.TextTrimming = Infragistics.Win.TextTrimming.EllipsisCharacter;
            this.ugServers.DisplayLayout.Override.CellAppearance = appearance17;
            this.ugServers.DisplayLayout.Override.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.CellSelect;
            this.ugServers.DisplayLayout.Override.CellMultiLine = Infragistics.Win.DefaultableBoolean.False;
            this.ugServers.DisplayLayout.Override.CellPadding = 0;
            this.ugServers.DisplayLayout.Override.ExpansionIndicator = Infragistics.Win.UltraWinGrid.ShowExpansionIndicator.Never;
            this.ugServers.DisplayLayout.Override.FixedRowIndicator = Infragistics.Win.UltraWinGrid.FixedRowIndicator.None;
            appearance18.BackColor = System.Drawing.SystemColors.Control;
            appearance18.BackColor2 = System.Drawing.SystemColors.ControlDark;
            appearance18.BackGradientAlignment = Infragistics.Win.GradientAlignment.Element;
            appearance18.BackGradientStyle = Infragistics.Win.GradientStyle.Horizontal;
            appearance18.BorderColor = System.Drawing.SystemColors.Window;
            this.ugServers.DisplayLayout.Override.GroupByRowAppearance = appearance18;
            appearance19.TextHAlign = Infragistics.Win.HAlign.Left;
            this.ugServers.DisplayLayout.Override.HeaderAppearance = appearance19;
            this.ugServers.DisplayLayout.Override.HeaderStyle = Infragistics.Win.HeaderStyle.WindowsXPCommand;
            appearance20.BackColor = System.Drawing.SystemColors.Window;
            appearance20.BorderColor = System.Drawing.Color.Silver;
            this.ugServers.DisplayLayout.Override.RowAppearance = appearance20;
            this.ugServers.DisplayLayout.Override.RowSelectorNumberStyle = Infragistics.Win.UltraWinGrid.RowSelectorNumberStyle.RowIndex;
            this.ugServers.DisplayLayout.Override.RowSelectors = Infragistics.Win.DefaultableBoolean.True;
            this.ugServers.DisplayLayout.Override.RowSelectorStyle = Infragistics.Win.HeaderStyle.WindowsXPCommand;
            this.ugServers.DisplayLayout.Override.ShowInkButton = Infragistics.Win.ShowInkButton.Never;
            appearance21.BackColor = System.Drawing.SystemColors.ControlLight;
            this.ugServers.DisplayLayout.Override.TemplateAddRowAppearance = appearance21;
            this.ugServers.DisplayLayout.PriorityScrolling = true;
            this.ugServers.DisplayLayout.ScrollBounds = Infragistics.Win.UltraWinGrid.ScrollBounds.ScrollToFill;
            this.ugServers.DisplayLayout.ScrollStyle = Infragistics.Win.UltraWinGrid.ScrollStyle.Immediate;
            this.ugServers.DisplayLayout.ViewStyle = Infragistics.Win.UltraWinGrid.ViewStyle.SingleBand;
            this.ugServers.Dock = System.Windows.Forms.DockStyle.Fill;
            this.ugServers.FlatMode = true;
            this.ugServers.Location = new System.Drawing.Point(3, 3);
            this.ugServers.Margin = new System.Windows.Forms.Padding(0);
            this.ugServers.Name = "ugServers";
            this.ugServers.Size = new System.Drawing.Size(848, 364);
            this.ugServers.TabIndex = 0;
            this.ugServers.Text = "独立运行";
            this.ugServers.ClickCellButton += new Infragistics.Win.UltraWinGrid.CellEventHandler(this.ugServers_ClickCellButton);
            this.ugServers.DoubleClickHeader += new Infragistics.Win.UltraWinGrid.DoubleClickHeaderEventHandler(this.ugServers_DoubleClickHeader);
            this.ugServers.AfterCellUpdate += new Infragistics.Win.UltraWinGrid.CellEventHandler(this.ugServers_AfterCellUpdate);
            this.ugServers.InitializeRow += new Infragistics.Win.UltraWinGrid.InitializeRowEventHandler(this.ugServers_InitializeRow);
            // 
            // dataSet1
            // 
            this.dataSet1.DataSetName = "NewDataSet";
            this.dataSet1.Tables.AddRange(new System.Data.DataTable[] {
            this.dataTable1});
            // 
            // dataTable1
            // 
            this.dataTable1.TableName = "Table1";
            // 
            // splitContainer1
            // 
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.IsSplitterFixed = true;
            this.splitContainer1.Location = new System.Drawing.Point(0, 0);
            this.splitContainer1.Margin = new System.Windows.Forms.Padding(0);
            this.splitContainer1.Name = "splitContainer1";
            this.splitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal;
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.pictureBox1);
            this.splitContainer1.Panel1MinSize = 100;
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.Controls.Add(this.tabControl1);
            this.splitContainer1.Size = new System.Drawing.Size(862, 513);
            this.splitContainer1.SplitterDistance = 116;
            this.splitContainer1.SplitterWidth = 1;
            this.splitContainer1.TabIndex = 3;
            // 
            // FrmSeverMain
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(862, 513);
            this.Controls.Add(this.splitContainer1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.Name = "FrmSeverMain";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "应用服务";
            this.Load += new System.EventHandler(this.FrmSeverMain_Load);
            this.Closing += new System.ComponentModel.CancelEventHandler(this.FrmSeverMain_Closing);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.tabControl1.ResumeLayout(false);
            this.tabPage1.ResumeLayout(false);
            this.tabPage2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.Chart1)).EndInit();
            this.tabPage3.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.Chart2)).EndInit();
            this.tabPage4.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.ugServers)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataTable1)).EndInit();
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            this.splitContainer1.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        #endregion

        private void FrmSeverMain_Load(object sender, System.EventArgs e)
        {
            ugValueList = GetServerVL();
            //==>
            //if (System.DateTime.Today > new System.DateTime(2007,1,1))	return;

            ApplicationStart();
            this.pictureBox1.Image = System.Drawing.Image.FromFile(@"Image\登录框.jpg");
            cMain = new ClsServerMain();

            cMain.ShowLogEvent += new Core.Mes.ServerManager.ClsServerMain.ShowLogDelegate(cMain_ShowLog);

            cMain._isLogging = IsLogging;
            Application.DoEvents();
            this.Show();

            InitConfigure();

            cMain.InitServerMain(new object[] { _serverConf });

            resDaemon = new ResDaemon();
            resDaemon.StartDaemon();

            srvDaemon = new SrvDaemon(cMain.HtAssemblyService);
            srvDaemon.StartDaemon();
            InitChart();

            extSrvDaemon = new ExtSrvDaemon();
            ///////////////服务启动后，方可进行资源监控/////////////////
            this.tabControl1.Controls.Add(this.tabPage2);
            this.tabControl1.Controls.Add(this.tabPage3);
            this.tabControl1.Controls.Add(this.tabPage4);
            this.tabControl1.Selected += new System.Windows.Forms.TabControlEventHandler(this.tabControl1_Selected);

            cMain_ShowLog("启动完成");
        }

        private void InitConfigure()
        {
            _serverConf = new ServerConfigure();
            _serverConf.ssc_event = new Core.Mes.ServerFrameWork.ExtSrvDaemon.ShowExtSrvEvent(CallBackShowExtSrv);
            _serverConf.OnMasterServerChanged = new InvokeCallback(MasterChanged);
        }

        private void MasterChanged(string _mName)
        {
            if (ugServers.InvokeRequired)
            {
                InvokeCallback method = new InvokeCallback(MasterChanged);
                ugServers.Invoke(method, _mName);
                return;
            }

            if (string.IsNullOrEmpty(_mName))
            {
                ugServers.Text = "独立运行";
            }
            else
            {
                UriBuilder ub = new UriBuilder(_mName);
                ugServers.Text = string.Format("本机受控于服务器[{0}:{1}]", ub.Host, ub.Port);
            }
        }

        Thread t_ShowRes = null;
        Thread t_ShowSrv = null;
        Thread t_ShowExt = null;
        private void InitChart()
        {
            Chart1.ChartAreas[0].AxisY.Minimum = 0;
            Chart1.ChartAreas[0].AxisX.Minimum = 0;

            Chart2.ChartAreas[0].AxisY.Minimum = 0;
            Chart2.ChartAreas[0].AxisY2.Minimum = 0;
            Chart2.ChartAreas[0].AxisY2.Maximum = 100;

            StopShowResData();
            StopShowSrvData();
        }

        private void StartShowResData()
        {
            if (t_ShowRes == null ||
                t_ShowRes.ThreadState == System.Threading.ThreadState.Stopped ||
                t_ShowRes.ThreadState == System.Threading.ThreadState.Aborted)
            {
                t_ShowRes = new Thread(ShowResData);
                t_ShowRes.Name = "资源数据读取";
                t_ShowRes.Start();
                resDaemon.ResumeMe();
            }
            else
            {
                resDaemon.ResumeMe();
            }
        }

        private void StartShowSrvData()
        {
            if (t_ShowSrv == null ||
                t_ShowSrv.ThreadState == System.Threading.ThreadState.Stopped ||
                t_ShowSrv.ThreadState == System.Threading.ThreadState.Aborted)
            {
                t_ShowSrv = new Thread(ShowSrvData);
                t_ShowSrv.Name = "服务数据读取";
                t_ShowSrv.Start();
                srvDaemon.ResumeMe();
            }
            else
            {
                srvDaemon.ResumeMe();
            }
        }

        private void StartShowExtServer()
        {
            if (t_ShowExt == null ||
                t_ShowExt.ThreadState == System.Threading.ThreadState.Stopped ||
                t_ShowExt.ThreadState == System.Threading.ThreadState.Aborted)
            {
                t_ShowExt = new Thread(ShowExtServer);
                t_ShowExt.Name = "扩展服务器读取";
                t_ShowExt.Start();
                extSrvDaemon.ResumeMe();
            }
            else
            {
                extSrvDaemon.ResumeMe();
            }
        }

        private void StopShowResData()
        {
            if (t_ShowRes != null)
            {
                resDaemon.SuspendMe();
            }
        }
        private void StopShowSrvData()
        {
            if (t_ShowSrv != null)
            {
                srvDaemon.SuspendMe();
            }
        }
        private void StopShowExtServer()
        {
            if (t_ShowSrv != null)
            {
                extSrvDaemon.SuspendMe();
            }
        }
        private void ShowResData()
        {
            try
            {
                resDaemon.ShowResData(this.Chart2, new ResDaemon.ShowResDataEvent(CallBackShowResData));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "资源显示线程错误");
            }
        }

        private void ShowSrvData()
        {
            try
            {
                srvDaemon.ShowSrvData(new SrvDaemon.ShowSrvDataEvent(CallBackShowSrvData));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "服务显示线程错误");
            }
        }

        private void ShowExtServer()
        {
            try
            {
                extSrvDaemon.ShowExtServer(new ExtSrvDaemon.ShowExtSrvEvent(CallBackShowExtSrv));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "扩展服务器显示线程错误");
            }
        }

        private void CallBackShowResData(List<DataPoint> ps1, List<DataPoint> ps2)
        {
            if (Chart2.InvokeRequired)
            {
                ResDaemon.ShowResDataEvent srde = new ResDaemon.ShowResDataEvent(CallBackShowResData);
                this.Invoke(srde, new object[] { ps1, ps2 });
                return;
            }
            Series s1 = Chart2.Series[0];
            Series s2 = Chart2.Series[1];

            s1.Points.Invalidate();
            s2.Points.Invalidate();

            s1.Points.Clear();
            s2.Points.Clear();

            foreach (DataPoint dp in ps1)
            {
                s1.Points.Add(dp);
            }
            foreach (DataPoint dp in ps2)
            {
                s2.Points.Add(dp);
            }

            Chart2.ChartAreas[0].AxisX.Minimum = s1.Points[0].XValue;
            Chart2.ChartAreas[0].AxisX.Maximum = s1.Points[s1.Points.Count - 1].XValue;

            SystemInfo si = new SystemInfo();
            Chart2.Legends[0].CustomItems[0].Cells[0].Text = (si.MemoryAvailable / 1024 / 1024).ToString();

            s1.Points.ResumeUpdates();
            s2.Points.ResumeUpdates();
        }

        private void CallBackShowSrvData(List<DataPoint> ps1, List<DataPoint> ps2)
        {
            if (Chart1.InvokeRequired)
            {
                SrvDaemon.ShowSrvDataEvent ssde = new SrvDaemon.ShowSrvDataEvent(CallBackShowSrvData);
                this.Invoke(ssde, new object[] { ps1, ps2 });
                return;
            }
            Series s1 = Chart1.Series[0];
            Series s2 = Chart1.Series[1];

            s1.Points.Invalidate();
            s2.Points.Invalidate();

            s1.Points.Clear();
            s2.Points.Clear();

            foreach (DataPoint dp in ps1)
            {
                s1.Points.Add(dp);
            }
            foreach (DataPoint dp in ps2)
            {
                s2.Points.Add(dp);
            }

            s1.Points.ResumeUpdates();
        }

        private void CallBackShowExtSrv()
        {
            if (ugServers.InvokeRequired)
            {
                ExtSrvDaemon.ShowExtSrvEvent sese = new ExtSrvDaemon.ShowExtSrvEvent(CallBackShowExtSrv);
                this.Invoke(sese);
                return;
            }
            ugServers.SuspendLayout();
            ugServers.SuspendRowSynchronization();

            Hashtable extServers = this.cMain.HtExternServer;
            DataTable dt = this.dataSet1.Tables[0];

            if (dt.Columns.Count == 0)
            {
                dt.Columns.Add("本地服务", typeof(string));
            }
            if (dt.Rows.Count == 0)
            {
                DataRow dr = dt.NewRow();
                dr[0] = "1";
                dt.Rows.Add(dr);
            }
            dt.AcceptChanges();

            //去除无效服务器
            foreach (DataColumn dc in dt.Columns)
            {
                if (dc.ColumnName == "本地服务") continue;
                bool keep_it = false;
                foreach (string ServerName in extServers.Keys)
                {
                    UriBuilder ub = new UriBuilder(ServerName);
                    string sn = string.Format("{0}:{1}", ub.Host, ub.Port);

                    if (sn == dc.ColumnName)
                    {
                        keep_it = true;
                    }
                }
                if (!keep_it)
                {
                    dt.Columns.Remove(dc);
                }
            }
            dt.AcceptChanges();

            //新增服务器
            foreach (string ServerName in extServers.Keys)
            {
                UriBuilder ub = new UriBuilder(ServerName);
                string sn = string.Format("{0}:{1}", ub.Host, ub.Port);
                if (!dt.Columns.Contains(sn))
                {
                    dt.Columns.Add(sn, typeof(string));
                }
                RemotingServer rs = (RemotingServer)(extServers[ServerName]);

                string status = (rs.Valid != RemoteServerStatus.Normal) ? ((int)(rs.Valid)).ToString() : (rs.Enable ? "1" : "3");
                dt.Rows[0][sn] = status;
            }

            //去除无效服务
            for (int idx = 1; idx < dt.Rows.Count; idx++)
            {
                DataRow dr = dt.Rows[idx];
                if (cMain.HTServiceKeys.ContainsKey(dr["本地服务"].ToString())) continue;

                dr.Delete();
            }
            dt.AcceptChanges();
            //新增服务项
            foreach (string serviceName in this.cMain.HTServiceKeys.Keys)
            {
                DataRow[] drs = dt.Select(string.Format("本地服务='{0}'", serviceName));
                DataRow dr = null;
                if (drs.GetLength(0) == 0)
                {
                    dr = dt.NewRow();
                    dr[0] = serviceName;
                    dt.Rows.Add(dr);
                }
                else
                {
                    dr = drs[0];
                }
                foreach (string serverName in extServers.Keys)
                {
                    UriBuilder ub = new UriBuilder(serverName);
                    string sn = string.Format("{0}:{1}", ub.Host, ub.Port);
                    RemotingServer rs = (RemotingServer)(extServers[serverName]);

                    string server_status = dt.Rows[0][sn].ToString();
                    if (server_status != "1")
                    {
                        dr[sn] = "1" + server_status;
                        continue;
                    }

                    string status = "1";
                    if (!rs.HTServices.ContainsKey(serviceName))
                    {
                        status = "25";
                    }
                    else
                    {
                        ServiceObject so = (ServiceObject)(rs.HTServices[serviceName]);
                        switch (so.Valid)
                        {
                            case ServiceObjectStatus.Normal:
                                status = (so.Enable ? "11" : "13");
                                break;
                            case ServiceObjectStatus.Unable:
                                status = "12";
                                break;
                            case ServiceObjectStatus.UnMatch:
                                status = "24";
                                break;
                        }
                    }

                    dr[sn] = status;
                }
            }
            dt.AcceptChanges();

            for (int idx = 0; idx < ugServers.DisplayLayout.Bands[0].Columns.Count; idx++)
            {
                ugServers.DisplayLayout.Bands[0].Columns[idx].ValueList = ugValueList;

            }

            ugServers.ResumeRowSynchronization();
            ugServers.ResumeLayout();
        }

        private static ValueList ugValueList = null;
        private ValueList GetServerVL()
        {
            ValueList vl = new ValueList();
            vl.ValueListItems.Add("1", "[正常运行]");
            vl.ValueListItems.Add("2", "[不可用]");
            vl.ValueListItems.Add("3", "[暂停服务]");
            vl.ValueListItems.Add("4", "[受控于其他主机]");
            vl.ValueListItems.Add("11", "功能正常");
            vl.ValueListItems.Add("12", "不可用");
            vl.ValueListItems.Add("13", "暂停服务");
            vl.ValueListItems.Add("14", "受控其他主机");
            vl.ValueListItems.Add("24", "版本不一致");
            vl.ValueListItems.Add("25", "缺少服务");
            return vl;
        }

        private void cMain_ShowLog(string log)
        {
            lock (this.listBox1)
            {
                this.listBox1.Invalidate();
                this.listBox1.Items.Add(log);
                this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1;
                Application.DoEvents();
                this.listBox1.Update();
            }
        }

        private void FrmSeverMain_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (MessageBox.Show("是否确认退出服务端程序？", "提示", MessageBoxButtons.YesNo,
                MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
            {
                e.Cancel = true;
            }
            else
            {
                ApplicationExit();
                Application.Exit();
                System.Diagnostics.Process.GetCurrentProcess().Kill();

            }
        }

        private static void ApplicationExit()
        {

            string path = string.Format(@"{0}/log/Application/{1}.txt", Application.StartupPath, System.DateTime.Now.ToString("yyyy_MM_dd"));

            using (StreamWriter sw = new StreamWriter(path, true, Encoding.UTF8))
            {
                StringBuilder sbtxt = new StringBuilder();
                sbtxt.AppendLine("==============================================");
                sbtxt.AppendLine(string.Format("服务关闭时间:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm::ss")));
                sbtxt.AppendLine("==============================================");
                sw.WriteLine(sbtxt.ToString());

            }


        }
        private static void ApplicationStart()
        {
            string path = string.Format(@"{0}/log/Application/{1}.txt", Application.StartupPath, System.DateTime.Now.ToString("yyyy_MM_dd"));

            using (StreamWriter sw = new StreamWriter(path, true, Encoding.UTF8))
            {
                StringBuilder sbtxt = new StringBuilder();
                sbtxt.AppendLine("==============================================");
                sbtxt.AppendLine(string.Format("服务开启时间:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm::ss")));
                sbtxt.AppendLine("==============================================");
                sw.WriteLine(sbtxt.ToString());

            }
        }

        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            if (e.TabPageIndex == 1)
            {
                StartShowSrvData();
            }
            else
            {
                StopShowSrvData();
            }

            if (e.TabPageIndex == 2)
            {
                StartShowResData();
            }
            else
            {
                StopShowResData();
            }

            if (e.TabPageIndex == 3)
            {
                StartShowExtServer();
            }
            else
            {
                StopShowExtServer();
            }

        }

        private void ugServers_AfterCellUpdate(object sender, CellEventArgs e)
        {
            UltraGridColumn uc = e.Cell.Column;

            try
            {
                if (uc.Index > 0 || e.Cell.Row.Index == 0)
                {
                    e.Cell.Appearance = ugServers.DisplayLayout.Appearances[e.Cell.Row.GetCellValue(uc).ToString()];
                }
            }
            catch { }
        }

        private void ugServers_InitializeRow(object sender, InitializeRowEventArgs e)
        {
            for (int idx = 0; idx < e.Row.Band.Columns.Count; idx++)
            {
                UltraGridColumn uc = e.Row.Band.Columns[idx];

                try
                {
                    if (uc.Index > 0 || e.Row.Index == 0)
                    {
                        e.Row.Cells[idx].Appearance = ugServers.DisplayLayout.Appearances[e.Row.GetCellValue(uc).ToString()];
                        string cell_value = e.Row.GetCellValue(uc).ToString();

                        if (cell_value == "1" || cell_value == "3" || cell_value == "11" || cell_value == "13")
                        {
                            e.Row.Cells[idx].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Button;
                        }
                        else
                        {
                            e.Row.Cells[idx].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Default;
                        }
                    }
                }
                catch { }
            }
        }

        private void ugServers_ClickCellButton(object sender, CellEventArgs e)
        {
            ugServers.SuspendRowSynchronization();
            UltraGridColumn uc = e.Cell.Column;
            string cell_value = e.Cell.Row.GetCellValue(uc).ToString();
            string ServerName = uc.Header.Caption;
            string ServiceName = e.Cell.Row.GetCellValue(e.Cell.Row.Band.Columns[0]).ToString();


            if (cell_value == "1")
            {
                cMain.SetServerFlag(ServerName, false);
            }
            else if (cell_value == "3")
            {
                cMain.SetServerFlag(ServerName, true);
            }
            else if (cell_value == "11")
            {
                cMain.SetServiceFlag(ServerName, ServiceName, false);
            }
            else if (cell_value == "13")
            {
                cMain.SetServiceFlag(ServerName, ServiceName, true);
            }
            ugServers.ResumeRowSynchronization();
            ugServers.Update();
        }

        private void ugServers_DoubleClickHeader(object sender, DoubleClickHeaderEventArgs e)
        {
            UriBuilder ub = new UriBuilder(e.Header.Caption);
            if (MessageBox.Show(string.Format("是否同步文件到服务器[{0}:{1}]?", ub.Host, ub.Port), "同步文件", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                == DialogResult.Yes)
            {
                try
                {
                    foreach (string ServerName in cMain.HtExternServer.Keys)
                    {
                        UriBuilder ube = new UriBuilder(ServerName);

                        if (ub.Host == ube.Host && ub.Port == ube.Port)
                        {
                            RemotingServer rs = (RemotingServer)(cMain.HtExternServer[ServerName]); 
                            //rs.SyncFiles();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}
