using System; using System.ComponentModel; using System.Drawing; using System.IO; using System.Threading; using System.Windows.Forms; namespace LP; public class FormBackupCurrent : Form { private delegate void delegateFormSetTitle(Form form, string value); private delegate void delegateListBoxItemsClear(ListBox listBox); private delegate void delegateListBoxAdd(ListBox listBox, string value); private delegate void delegateListBoxSelectedIndex(ListBox listBox, int value); private delegate void delegateTextBoxSetText(TextBox textBox, string value); private delegate void delegateButtonEnabled(Button button, bool value); private delegate void delegateFormHide(Form form); public FormDesktop desktop = null; public string mTitle = ""; public XianLuData mXianLuData = null; public string mNameXianLu = ""; public int mIndexControl_XianLuName = -1; public int mIndex_ListXianLuData = -1; public string[] mFileCurrentArr = null; private bool mAfterInit = false; private bool mReady_XianLuName = false; private bool mReady_CurrentFile = false; private bool mBusy_ButtonOK = false; private bool mComputing_Start = false; private ManualResetEvent manu = null; private CancellationTokenSource mCTS = null; private CancellationToken cancellationToken; private IContainer components = null; private Button buttonCancel; private Button buttonSelect; private Button buttonOK; private TextBox textBox_DS_FileName; private ListBox listBox_XianLuName; private Label label2; private Label label1; public bool Ready_XianLuName { get { return mReady_XianLuName; } set { mReady_XianLuName = value; } } public bool Ready_CurrentFile { get { return mReady_CurrentFile; } set { mReady_CurrentFile = value; } } public bool Busy_ButtonOK { get { return mBusy_ButtonOK; } set { mBusy_ButtonOK = value; MyButtonEnabled(buttonCancel, !value); MyButtonEnabled(buttonSelect, !value && !Computing_Start); } } public bool Computing_Start { get { return mComputing_Start; } set { mComputing_Start = value; MyButtonEnabled(buttonSelect, !value && !Busy_ButtonOK); } } public FormBackupCurrent() { InitializeComponent(); if (mXianLuData == null) { mXianLuData = new XianLuData(); } mTitle = "雷电流幅值统计"; mIndexControl_XianLuName = -1; mIndex_ListXianLuData = -1; manu = new ManualResetEvent(initialState: false); } private void FormBackupCurrent_Load(object sender, EventArgs e) { mAfterInit = false; Computing_Start = false; Busy_ButtonOK = false; MyFormSetTitle(this, ""); bool ready_CurrentFile = (Ready_XianLuName = false); Ready_CurrentFile = ready_CurrentFile; base.DialogResult = DialogResult.None; MyInput_List_XianLuData(); mAfterInit = true; } private void FormCurrent_FormClosing(object sender, FormClosingEventArgs e) { } public void MyInput_List_XianLuData() { if (mCTS == null) { mCTS = new CancellationTokenSource(); cancellationToken = mCTS.Token; manu.Reset(); } MyListBoxItemsClear(listBox_XianLuName); if (desktop.mXL != null && desktop.mXL.mListXianLuData != null) { for (int i = 0; i < desktop.mXL.mListXianLuData.Count; i++) { MyListBoxAdd(listBox_XianLuName, desktop.mXL.mListXianLuData[i].mName_XianLu); } if (mIndexControl_XianLuName < 0) { mIndexControl_XianLuName = 0; } if (mIndexControl_XianLuName >= 0 && mIndexControl_XianLuName < listBox_XianLuName.Items.Count) { MyListBoxSelectedIndex(listBox_XianLuName, mIndexControl_XianLuName); Ready_XianLuName = true; } } } public void Destroy() { if (mXianLuData != null) { mXianLuData.Destroy(); } mXianLuData = null; if (mCTS != null) { mCTS.Dispose(); } mCTS = null; if (mFileCurrentArr != null) { for (int i = 0; i < mFileCurrentArr.Length; i++) { mFileCurrentArr[i] = ""; } mFileCurrentArr = null; } manu.Dispose(); manu.Close(); } private void buttonOK_Click(object sender, EventArgs e) { MyFormSetTitle(this, ""); if (Busy_ButtonOK) { return; } if (Computing_Start) { MyFormSetTitle(this, "计算中。。。"); return; } Busy_ButtonOK = true; Computing_Start = false; if (!Ready_XianLuName) { if (listBox_XianLuName.Items.Count > 0) { MyFormSetTitle(this, "请选择线路,然后点击按钮“" + buttonOK.Text + "”。"); } if (listBox_XianLuName.Items.Count == 0) { MyFormSetTitle(this, "缺少线路数据。 请点击按钮“" + buttonCancel.Text + "”,退出对话框。"); } listBox_XianLuName.Focus(); Busy_ButtonOK = false; } else if (!Ready_CurrentFile) { MyFormSetTitle(this, "请选择雷电文件,点击按钮“" + buttonOK.Text + "”。"); buttonSelect.Focus(); Busy_ButtonOK = false; } else { Thread thread = new Thread(MyCompute); thread.Start(); } } private void MyCompute() { int num = 0; Computing_Start = true; Busy_ButtonOK = false; while (!cancellationToken.IsCancellationRequested) { num++; MyFormSetTitle(this, $"计算:第{num}次显示"); Thread.Sleep(1000); if (num >= 9) { break; } } if (cancellationToken.IsCancellationRequested) { manu.Set(); return; } MyFormSetTitle(this, "计算完成。"); MessageBox.Show("完成: " + mTitle + "。"); mCTS.Dispose(); mCTS = null; manu.Reset(); base.DialogResult = DialogResult.OK; MyFormHide(this); } private void buttonCancel_Click(object sender, EventArgs e) { MyFormSetTitle(this, ""); MyButtonEnabled(buttonCancel, value: false); Thread thread = new Thread(MyCancel); thread.Start(); } private void MyCancel() { MyFormSetTitle(this, "取消—"); if (Computing_Start) { mCTS.Cancel(); MyFormSetTitle(this, "取消——"); manu.WaitOne(); } MyFormSetTitle(this, "取消———"); mCTS.Dispose(); mCTS = null; manu.Reset(); base.DialogResult = DialogResult.Cancel; MyFormHide(this); } private void buttonSelect_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.InitialDirectory = Application.StartupPath; openFileDialog.Filter = "所有文件*.*|*.*|Excel文件(*.csv)|*.csv|Txt文件(*.txt)|*.txt"; openFileDialog.FilterIndex = 2; openFileDialog.Multiselect = true; if (openFileDialog.ShowDialog(this) != DialogResult.OK) { return; } mFileCurrentArr = openFileDialog.FileNames; if (mFileCurrentArr != null) { string text = ""; for (int i = 0; i < mFileCurrentArr.Length; i++) { text = text + mFileCurrentArr[i] + " "; } MyTextBoxSetText(textBox_DS_FileName, text); Ready_CurrentFile = true; } } private void listBox_XianLuName_SelectedIndexChanged(object sender, EventArgs e) { if (!mAfterInit || Busy_ButtonOK) { return; } int selectedIndex = ((ListBox)sender).SelectedIndex; mIndexControl_XianLuName = selectedIndex; mNameXianLu = ((ListBox)sender).SelectedItem.ToString().Trim(); for (int i = 0; i < desktop.mXL.mListXianLuData.Count; i++) { if (mNameXianLu.Equals(desktop.mXL.mListXianLuData[i].mName_XianLu)) { mIndex_ListXianLuData = i; mXianLuData.CopyFrom_XianLuData(desktop.mXL.mListXianLuData[i]); Ready_XianLuName = true; break; } } } private void textBox_DS_FileName_TextChanged(object sender, EventArgs e) { } private void textBox_DS_FileName_Leave(object sender, EventArgs e) { if (!mAfterInit || Busy_ButtonOK) { return; } Ready_CurrentFile = false; string text = textBox_DS_FileName.Text.Trim(); string[] separator = new string[4] { "\r", "\n", " ", ";" }; string[] array = null; if (string.IsNullOrEmpty(text)) { return; } array = text.Split(separator, StringSplitOptions.RemoveEmptyEntries); bool ready_CurrentFile = true; for (int i = 0; i < array.Length; i++) { if (!File.Exists(array[i])) { ready_CurrentFile = false; break; } } Ready_CurrentFile = ready_CurrentFile; } public void MyFormSetTitle(Form form, string value) { if (form.InvokeRequired) { delegateFormSetTitle method = MyFormSetTitle; form.Invoke(method, form, value); return; } if (string.IsNullOrEmpty(value)) { form.Text = mTitle; } if (!string.IsNullOrEmpty(value)) { form.Text = mTitle + " " + value; } } public void MyListBoxItemsClear(ListBox listBox) { if (listBox.InvokeRequired) { delegateListBoxItemsClear method = MyListBoxItemsClear; listBox.Invoke(method, listBox); } else { listBox.Items.Clear(); } } public void MyListBoxAdd(ListBox listBox, string value) { if (listBox.InvokeRequired) { delegateListBoxAdd method = MyListBoxAdd; listBox.Invoke(method, listBox, value); } else if (!string.IsNullOrEmpty(value)) { listBox.Items.Add(value); } } public void MyListBoxSelectedIndex(ListBox listBox, int value) { if (listBox.InvokeRequired) { delegateListBoxSelectedIndex method = MyListBoxSelectedIndex; listBox.Invoke(method, listBox, value); } else if (value >= 0 && value < listBox.Items.Count) { listBox.SelectedIndex = value; } } public void MyTextBoxSetText(TextBox textBox, string value) { if (textBox.InvokeRequired) { delegateTextBoxSetText method = MyTextBoxSetText; textBox.Invoke(method, textBox, value); } else { textBox.Text = value; } } public void MyButtonEnabled(Button button, bool value) { if (button.InvokeRequired) { delegateButtonEnabled method = MyButtonEnabled; button.Invoke(method, button, value); } else { button.Enabled = value; } } public void MyFormHide(Form form) { try { if (form.InvokeRequired) { delegateFormHide method = MyFormHide; form.Invoke(method, form); } else { form.Hide(); } } catch (Exception) { } } protected override void Dispose(bool disposing) { if (disposing && components != null) { components.Dispose(); } base.Dispose(disposing); } private void InitializeComponent() { this.buttonCancel = new System.Windows.Forms.Button(); this.buttonSelect = new System.Windows.Forms.Button(); this.buttonOK = new System.Windows.Forms.Button(); this.textBox_DS_FileName = new System.Windows.Forms.TextBox(); this.listBox_XianLuName = new System.Windows.Forms.ListBox(); this.label2 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); base.SuspendLayout(); this.buttonCancel.Location = new System.Drawing.Point(266, 183); this.buttonCancel.Name = "buttonCancel"; this.buttonCancel.Size = new System.Drawing.Size(108, 28); this.buttonCancel.TabIndex = 63; this.buttonCancel.Text = "取消"; this.buttonCancel.UseVisualStyleBackColor = true; this.buttonCancel.Click += new System.EventHandler(buttonCancel_Click); this.buttonSelect.Location = new System.Drawing.Point(325, 133); this.buttonSelect.Name = "buttonSelect"; this.buttonSelect.Size = new System.Drawing.Size(49, 28); this.buttonSelect.TabIndex = 61; this.buttonSelect.Text = "选择"; this.buttonSelect.UseVisualStyleBackColor = true; this.buttonSelect.Click += new System.EventHandler(buttonSelect_Click); this.buttonOK.Location = new System.Drawing.Point(131, 183); this.buttonOK.Name = "buttonOK"; this.buttonOK.Size = new System.Drawing.Size(108, 28); this.buttonOK.TabIndex = 62; this.buttonOK.Text = "确定"; this.buttonOK.UseVisualStyleBackColor = true; this.buttonOK.Click += new System.EventHandler(buttonOK_Click); this.textBox_DS_FileName.Font = new System.Drawing.Font("宋体", 12f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134); this.textBox_DS_FileName.Location = new System.Drawing.Point(18, 135); this.textBox_DS_FileName.Name = "textBox_DS_FileName"; this.textBox_DS_FileName.Size = new System.Drawing.Size(290, 26); this.textBox_DS_FileName.TabIndex = 60; this.textBox_DS_FileName.Leave += new System.EventHandler(textBox_DS_FileName_Leave); this.listBox_XianLuName.Font = new System.Drawing.Font("宋体", 12f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134); this.listBox_XianLuName.FormattingEnabled = true; this.listBox_XianLuName.ItemHeight = 16; this.listBox_XianLuName.Location = new System.Drawing.Point(18, 42); this.listBox_XianLuName.Name = "listBox_XianLuName"; this.listBox_XianLuName.Size = new System.Drawing.Size(356, 20); this.listBox_XianLuName.TabIndex = 59; this.listBox_XianLuName.SelectedIndexChanged += new System.EventHandler(listBox_XianLuName_SelectedIndexChanged); this.label2.AutoSize = true; this.label2.Font = new System.Drawing.Font("宋体", 12f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134); this.label2.Location = new System.Drawing.Point(18, 103); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(104, 16); this.label2.TabIndex = 57; this.label2.Text = "雷电地闪数据"; this.label1.AutoSize = true; this.label1.Font = new System.Drawing.Font("宋体", 12f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134); this.label1.Location = new System.Drawing.Point(18, 17); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(72, 16); this.label1.TabIndex = 58; this.label1.Text = "线路名称"; base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f); base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; base.ClientSize = new System.Drawing.Size(392, 228); base.Controls.Add(this.buttonCancel); base.Controls.Add(this.buttonSelect); base.Controls.Add(this.buttonOK); base.Controls.Add(this.textBox_DS_FileName); base.Controls.Add(this.listBox_XianLuName); base.Controls.Add(this.label2); base.Controls.Add(this.label1); base.Name = "FormBackupCurrent"; this.Text = "FormBackupCurrent"; base.Load += new System.EventHandler(FormBackupCurrent_Load); base.ResumeLayout(false); base.PerformLayout(); } }