using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Threading; using System.IO; using Newtonsoft.Json; namespace XS_BLL { public partial class DisplaySet : UserControl { #region 全局参数 /// /// 展示数据 /// public static string jsonData = string.Empty; /// /// 是否展示数据 /// public static bool blRb = false; public static List information = new List(); public static List errorDate = new List(); public static List FileExhibitionDate = new List(); /// /// 刷新数据 /// public static bool bRefresh = false; #endregion public DisplaySet() { InitializeComponent(); } #region /// /// 日志展示 /// /// 接口名 /// 值1 /// 值2 public void AddListBoxItem(string name, string value, string value2 = "") { information.Insert(0, name + "-->>" + value + value2); } public void ErrorAddListBoxItem(string name, string value, string value2 = "") { errorDate.Insert(0, name + "-->>" + value + value2); } void show() { //异步外的方法。这样窗体不会假死 while (blRb) { blRb = false; Thread.Sleep(10); Action ac = new Action(showText); this.Invoke(ac); //在同步方法里面实现更新窗体上的数据 } } /// /// 更新数据 /// void showText() { string[] resAry = jsonData.Split(new string[] { "\r\n" }, StringSplitOptions.None); string tex = string.Empty; if (resAry.Length > 1) { tex = DateTime.Now + "\r\n" + resAry[0] + "\r\n" + UtilityBLL.JsonToString(resAry[resAry.Length - 1]); for (int i = 0; i < resAry.Length; i++) { tex += DateTime.Now + "\r\n" + resAry[i] + "\r\n" + UtilityBLL.JsonToString(resAry[i]); } } else { tex = DateTime.Now + "\r\n" + UtilityBLL.JsonToString(jsonData); } richTextBox1.Text = tex; } #endregion private void DisplaySet_Load(object sender, EventArgs e) { new Thread(() => RefreshDeviceStatesAndTasks()) { IsBackground = true }.Start(); } private void RefreshDeviceStatesAndTasks() { while (true) { try { if (bRefresh || string.IsNullOrEmpty(jsonData) || jsonData.Replace("\r\n", "").Length < 20) { this.richTextBox1.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } else { if (this.richTextBox1.Text.Replace("\r\n", "").Length < 20) { this.richTextBox1.Text = !string.IsNullOrEmpty(jsonData) ? jsonData : DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } } if (bRefresh || lb_Message.Items.Count > 1000) { lb_Message.Items.Clear(); } if (information.Count > 0) { lb_Message.Items.Insert(0, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "-->>" + information[information.Count - 1]); information.RemoveAt(information.Count - 1); } if (bRefresh || lb_error.Items.Count > 200) { lb_error.Items.Clear(); } if (errorDate.Count > 0) { lb_error.Items.Insert(0, DateTime.Now.ToString() + "-->>" + errorDate[errorDate.Count - 1]); errorDate.RemoveAt(errorDate.Count - 1); } if (FileExhibitionDate.Count <= 0) { lb_FileExhibition.Items.Clear(); } else { if (lb_FileExhibition.Items.Count >= 0) { lb_FileExhibition.DataSource = FileExhibitionDate; } } if (bRefresh) { bRefresh = false; } Thread.Sleep(10); } catch (Exception ex) { } } } private void Lb_Message_MouseClick(object sender, MouseEventArgs e) { if (lb_Message.SelectedIndex >= 0) { string txt = lb_Message.Items[lb_Message.SelectedIndex].ToString(); if (!string.IsNullOrEmpty(txt)) { Clipboard.SetText(txt); } } DisplayError(); } public void DisplayError(bool bl = true) { //splitContainer1.Panel2Collapsed = bl; } private void Lb_Message_MouseDoubleClick(object sender, MouseEventArgs e) { if (lb_Message.SelectedIndex >= 0) { string txt = lb_Message.Items[lb_Message.SelectedIndex].ToString(); string[] resAry = txt.Split(new string[] { "-->>" }, StringSplitOptions.None); StringBuilder sb = new StringBuilder(); for (int i = 0; i < resAry.Length; i++) { if (i == 0) { sb.AppendLine(resAry[0]); } else { sb.AppendLine(UtilityBLL.JsonToString(resAry[i])); } } richTextBox1.Clear(); jsonData = sb.ToString(); } DisplayError(false); } public void eliminate() { bRefresh = true; information.Clear(); jsonData = string.Empty; } private void Lb_error_MouseClick(object sender, MouseEventArgs e) { if (lb_error.SelectedIndex >= 0) { string txt = lb_error.Items[lb_error.SelectedIndex].ToString(); if (!string.IsNullOrEmpty(txt)) { Clipboard.SetText(txt); } } } private void Lb_error_MouseDoubleClick(object sender, MouseEventArgs e) { if (lb_error.SelectedIndex >= 0) { string txt = lb_error.Items[lb_error.SelectedIndex].ToString(); string[] resAry = txt.Split(new string[] { "-->>" }, StringSplitOptions.None); StringBuilder sb = new StringBuilder(); for (int i = 0; i < resAry.Length; i++) { if (i == 0) { sb.AppendLine(resAry[0]); } else { sb.AppendLine(UtilityBLL.JsonToString(resAry[i])); } } richTextBox1.Clear(); jsonData = sb.ToString(); } } /// /// 文件读取显示 /// public void FileExhibition() { try { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "所有文件(*.*)|*.*"; string file = string.Empty; //设置默认打开路径(绝对路径) dialog.InitialDirectory = file; //ofd.Filter = "Excel文件(*.xls,*.xlsx)|*.xls;*.xlsx"; dialog.RestoreDirectory = true; string myStr = string.Empty; if (dialog.ShowDialog() == DialogResult.OK) { file = dialog.FileName; //if (!Directory.Exists(file)) //{ // return; //} FileStream fsm = new FileStream(file, FileMode.Open, FileAccess.Read); try { byte[] bt = new byte[fsm.Length]; fsm.Read(bt, 0, bt.Length); myStr = System.Text.Encoding.UTF8.GetString(bt); } catch (Exception ex) { myStr = ""; } finally { if (fsm != null) { fsm.Close(); } } } else { MessageBox.Show("文件无有效数据或文件被占用!!!", "提示:"); return; } FileExhibitionDate = myStr.Split('\n').ToList(); List list = myStr.Split('\n').ToList(); lb_FileExhibition.DataSource = FileExhibitionDate; } catch (Exception ex) { } } private void Lb_FileExhibition_MouseClick(object sender, MouseEventArgs e) { if (lb_FileExhibition.SelectedIndex >= 0) { string txt = lb_FileExhibition.Items[lb_FileExhibition.SelectedIndex].ToString(); if (!string.IsNullOrEmpty(txt)) { Clipboard.SetText(txt); } } } private void Lb_FileExhibition_MouseDoubleClick(object sender, MouseEventArgs e) { if (lb_FileExhibition.SelectedIndex >= 0) { richTextBox1.Clear(); string txt = string.Empty; List txtlist = new List(); string temp = lb_FileExhibition.Items[lb_FileExhibition.SelectedIndex].ToString(); if (temp.IndexOf('{') > 0 && temp.IndexOf('}') > 0) { txtlist.Add(lb_FileExhibition.Items[lb_FileExhibition.SelectedIndex].ToString()); txtlist.Add(txtlist[0].Substring(0, txtlist[0].IndexOf('{'))); txtlist.Add(txtlist[0].Substring(txtlist[0].IndexOf('{'), txtlist[0].LastIndexOf('}') + 1 - txtlist[0].IndexOf('{'))); txtlist.Add(txtlist[0].Substring(txtlist[0].LastIndexOf('}') + 1, txtlist[0].Length - txtlist[0].LastIndexOf('}') - 1)); //格式化json字符串 JsonSerializer serializer = new JsonSerializer(); TextReader tr = new StringReader(txtlist[2]); JsonTextReader jtr = new JsonTextReader(tr); object obj = serializer.Deserialize(jtr); if (obj != null) { StringWriter textWriter = new StringWriter(); JsonTextWriter jsonWriter = new JsonTextWriter(textWriter) { Formatting = Formatting.Indented, Indentation = 4, IndentChar = ' ' }; serializer.Serialize(jsonWriter, obj); txt = textWriter.ToString(); } else { txt = txtlist[0]; } jsonData = txtlist[1] + "\r\n" + txt + "\r\n" + txtlist[3]; } else { jsonData = temp; } } } private void Lb_FileExhibition_SelectedIndexChanged(object sender, EventArgs e) { Thread t = new Thread((ThreadStart)(() => { string txt = string.Empty; foreach (var temp in lb_FileExhibition.SelectedItems) { txt += temp.ToString(); //string Value = drv[lb_FileExhibition.ValueMember].ToString(); } if (!string.IsNullOrEmpty(txt)) { Clipboard.SetText(txt); } })); t.SetApartmentState(ApartmentState.STA); t.Start(); t.Join(); } } }