private void LoadResult(TaskData data) { if (data == null) { return; } btnRun.Enabled = !data.Executing; if (data.Executing) { grdResult.Clear(); txtResult.Clear(); lblResultCount.Visible = false; lblElapsed.Text = Resources.Running; prgRunning.Style = ProgressBarStyle.Marquee; } else { lblResultCount.Visible = true; lblElapsed.Text = data.Elapsed.ToString(); prgRunning.Style = ProgressBarStyle.Blocks; lblResultCount.Text = data.Result == null ? "" : data.Result.Rows.Count == 0 ? Resources.NoDocuments : data.Result.Rows.Count == 1 ? Resources._1Document : data.Result.Rows.Count + (data.LimitExceeded ? "+" : "") + Resources.Documents; if (data.Exception != null) { txtResult.BindErrorMessage(data.Sql, data.Exception); grdResult.BindErrorMessage(data.Sql, data.Exception); } else if (data.Result != null) { if (tabResult.SelectedTab == tabGrid && data.IsGridLoaded == false) { grdResult.BindBsonData(chartMain, data, txtResult); grdResult.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); data.IsGridLoaded = true; } else if (tabResult.SelectedTab == tabText && data.IsTextLoaded == false) { txtResult.AppendLine($"{DateTime.Now}-开始时间:{data.StartDateTime}\r\b\t\t耗时:{data.EndDateTime.Subtract(data.StartDateTime)}\r\b\t\t共计:{data.Result.Columns.Count}列{data.Result.Rows.Count}调数据 "); data.IsTextLoaded = true; } } } }
private void LoadResult(TaskData data) { if (data == null) { return; } btnRun.Enabled = !data.Executing; if (data.Executing) { grdResult.Clear(); txtResult.Clear(); lblResultCount.Visible = false; lblElapsed.Text = "Running"; prgRunning.Style = ProgressBarStyle.Marquee; } else { lblResultCount.Visible = true; lblElapsed.Text = data.Elapsed.ToString(); prgRunning.Style = ProgressBarStyle.Blocks; lblResultCount.Text = data.Result == null ? "" : data.Result.Count == 0 ? "no documents" : data.Result.Count == 1 ? "1 document" : data.Result.Count + (data.LimitExceeded ? "+" : "") + " documents"; if (data.Exception != null) { txtResult.BindErrorMessage(data.Sql, data.Exception); grdResult.BindErrorMessage(data.Sql, data.Exception); } else if (data.Result != null) { if (tabResult.SelectedTab == tabGrid && data.IsGridLoaded == false) { grdResult.BindBsonData(data); data.IsGridLoaded = true; } else if (tabResult.SelectedTab == tabText && data.IsTextLoaded == false) { txtResult.BindBsonData(data); data.IsTextLoaded = true; } } } }
public static void BindBsonData(this DataGridView grd, Chart chart, TaskData data, TextEditorControl text) { text.AppendLine($"{DateTime.Now}-准备数据.."); grd.Clear(); text.Clear(); chart.Series.Clear(); grd.DataSource = data.Result; DataTable dt = data.Result?.Copy().RemoveDataTableNullColumns(); chart.DataSource = dt; text.AppendLine($"{DateTime.Now}-合成曲线图.."); if (dt?.Columns.OfType <DataColumn>().Any(col => col.ColumnName == "ts") == true) { foreach (DataColumn col in dt.Columns) { if (col.ColumnName != "ts") { if (col.DataType == typeof(long)) { chart.AddSeries(col.ColumnName, ChartValueType.Int64); } else if (col.DataType == typeof(int)) { chart.AddSeries(col.ColumnName, ChartValueType.Int32); } else if (col.DataType == typeof(float)) { chart.AddSeries(col.ColumnName, ChartValueType.Single); } else if (col.DataType == typeof(double)) { chart.AddSeries(col.ColumnName, ChartValueType.Double); } } } chart.DataBind(); } if (data.LimitExceeded) { DataGridViewRow limitRow = new DataGridViewRow(); limitRow.CreateCells(grd); limitRow.DefaultCellStyle.ForeColor = Color.OrangeRed; DataGridViewCell cell = limitRow.Cells[0]; cell.Value = Resources.LimitExceeded; cell.ReadOnly = true; grd.Rows.Add(limitRow); } for (int i = 0; i <= grd.Columns.Count - 1; i++) { int colw = grd.Columns[i].Width; grd.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None; grd.Columns[i].Width = Math.Min(colw, 400); } if (grd.Rows.Count == 0) { grd.Columns.Add("no-data", Resources.NoResult); } grd.ReadOnly = grd.Columns["_id"] == null; grd.Visible = true; text.AppendLine($"{DateTime.Now}-完成.."); }
public static void BindParameter(this ICSharpCode.TextEditor.TextEditorControl txt, TaskData data) { //txt.SuspendLayout(); //txt.Clear(); //txt.SetHighlighting("JSON"); //var sb = new StringBuilder(); //using (var writer = new StringWriter(sb)) //{ // var w = new JsonWriter(writer) // { // Pretty = true, // Indent = 2 // }; // w.Serialize(data.Parameters ?? BsonValue.Null); //} //txt.Text = sb.ToString(); //txt.ResumeLayout(); }
public static void BindBsonData(this DataGridView grd, TaskData data) { // hide grid if has more than 100 rows grd.Visible = data.Result.Count < 100; grd.Clear(); foreach (var doc in data.Result) { var row = new DataGridViewRow(); foreach (JProperty key in doc.Children()) { var col = grd.Columns[key.Name]; if (col == null) { grd.Columns.Add(key.Name, key.Name); col = grd.Columns[key.Name]; col.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; col.ReadOnly = key.Path == "ts"; } } row.DefaultCellStyle.BackColor = Color.Silver; row.CreateCells(grd); foreach (JProperty key in doc.Children()) { var col = grd.Columns[key.Name]; var cell = row.Cells[col.Index]; cell.Style.BackColor = Color.White; cell.SetBsonValue(key.Value); row.ReadOnly = key.Name == "ts"; } grd.Rows.Add(row); } if (data.LimitExceeded) { var limitRow = new DataGridViewRow(); limitRow.CreateCells(grd); limitRow.DefaultCellStyle.ForeColor = Color.OrangeRed; var cell = limitRow.Cells[0]; cell.Value = "Limit exceeded"; cell.ReadOnly = true; grd.Rows.Add(limitRow); } for (int i = 0; i <= grd.Columns.Count - 1; i++) { var colw = grd.Columns[i].Width; grd.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None; grd.Columns[i].Width = Math.Min(colw, 400); } if (grd.Rows.Count == 0) { grd.Columns.Add("no-data", "[no result]"); } grd.ReadOnly = grd.Columns["_id"] == null; grd.Visible = true; }
public static void BindBsonData(this ICSharpCode.TextEditor.TextEditorControl txt, TaskData data) { var index = 0; var sb = new StringBuilder(); if (data.Result.Count > 0) { foreach (var value in data.Result) { if (data.Result?.Count > 1) { sb.AppendLine($"/* {index++ + 1} */"); } sb.Append(JsonConvert.SerializeObject(value)); sb.AppendLine(); } if (data.LimitExceeded) { sb.AppendLine(); sb.AppendLine("/* Limit exceeded */"); } } else { sb.AppendLine("no result"); } txt.SetHighlighting("JSON"); txt.Text = sb.ToString(); }
private void CreateThread(TaskData task) { while (true) { task.WaitHandle.Wait(); if (task.ThreadRunning == false) { break; } if (task.Sql.Trim() == "") { task.WaitHandle.Reset(); continue; } var sw = new Stopwatch(); sw.Start(); try { task.Executing = true; task.IsGridLoaded = task.IsTextLoaded = task.IsParametersLoaded = false; _synchronizationContext.Post(new SendOrPostCallback(o => { this.LoadResult(task); }), task); task.Parameters = null; task.StartDateTime = DateTime.Now; using (var reader = _db.CreateCommand(task.Sql).ExecuteReader()) { task.ReadResult(reader); } task.EndDateTime = DateTime.Now; task.Elapsed = sw.Elapsed; task.Exception = null; task.Executing = false; // update form button selected _synchronizationContext.Post(new SendOrPostCallback(o => { var t = o as TaskData; if (this.ActiveTask?.Id == t.Id) { this.LoadResult(o as TaskData); } }), task); } catch (Exception ex) { task.Executing = false; task.Result = null; task.Elapsed = sw.Elapsed; task.Exception = ex; _synchronizationContext.Post(new SendOrPostCallback(o => { var t = o as TaskData; if (this.ActiveTask?.Id == t.Id) { tabResult.SelectedTab = tabText; this.LoadResult(o as TaskData); } }), task); } // put thread in wait mode task.WaitHandle.Reset(); } task.WaitHandle.Dispose(); }