在C#项目中更新表导致崩溃

编程入门 行业动态 更新时间:2024-10-27 16:27:15
本文介绍了在C#项目中更新表导致崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图在我的C#项目中更新一个包含多列的表。我的目的是定期(4秒)从电子设备中记录值。我能够根据需要更新表并将表值保存在.excel文件中。 它工作正常一段时间但是在一段时间之后项目将卡住,要么我必须关闭或重启PC。如果我不更新表格,那么该项目可以正常工作几天。 我有一个从表值中绘制图表的代码,即使我尝试没有图表绘制或没有更新图表功能,只有更新表,即使它是同样的问题 它将工作5个小时,每4秒更新一次表。 我有一个代码来更新这样的表。

Hi, I trying to update a table with multiple columns in my C# project.My intention is to log values from electronics at regular interval(4sec). I was able to update table as i want and saving table value in .excel file. It works fine for some time but after sometime the project will stuck, either i have to close or restart the PC. If i don't update the table then the project works fine for days. I have a code to draw chart from table values, even i tried without chart drawing or without update chart function and only updating table even though it was same problem It will work for 5 hours, updating table every 4 sec. I have a code to update table like this.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using Steema.TeeChart; using Steema.TeeChart.Styles; using System.Windows.Forms; using NativeExcel; namespace SensorLog { public class Log { private TChart _chart; private DataGridView _list; private DataTable _table = new DataTable("EngineeringData"); private string _logFile; private long _recs = 0; private int _logs = 0; private IWorkbook book; private IWorksheet sheet; public Log(TChart chart, DataGridView list) { _chart = chart; _list = list; _list.DataSource = _table; MakeTable(); book = Factory.CreateWorkbook(); _logFile = "Log_" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Hour.ToString() + "-" + DateTime.Now.Minute.ToString() + "_.xls"; } public void Start() { _table.Clear(); _recs = 0; sheet = book.Worksheets.Add(); AnalogSensor.NewAnalogSensorData += new EventHandler<AnalogSensorDataEventArgs>(AnalogSensor_NewAnalogSensorData); } public void Stop() { int i = 1; foreach (DataColumn c in _table.Columns) { sheet.Cells[1, i].Value = c.ColumnName; sheet.Cells[2, i++].Value = c.Caption; } book.SaveAs(_logFile); AnalogSensor.NewAnalogSensorData -= new EventHandler<AnalogSensorDataEventArgs>(AnalogSensor_NewAnalogSensorData); } void AnalogSensor_NewAnalogSensorData(object sender, AnalogSensorDataEventArgs e) { if (e.Data.Priority > 0) { if (_list.InvokeRequired) _list.Invoke((MethodInvoker)delegate { UpdateTable(e.Data); }); else UpdateTable(e.Data); UpdateChart(e.Data); } } private void MakeTable() { DataColumn[] keys = new DataColumn[1]; Type type = Type.GetType("System.UInt64"); keys[0] = MakeColumn("Index", type, true); _table.PrimaryKey = keys; type = Type.GetType("System.DateTime"); MakeColumn("DateTime", type, true); _list.Columns["DateTime"].Visible = false; type = Type.GetType("System.String"); MakeColumn("Time", type, false); type = Type.GetType("System.Double"); MakeColumn(SystemNames.PressA, type, false); MakeColumn(SystemNames.PressB, type, false); MakeColumn(SystemNames.AnalogA, type, false); MakeColumn(SystemNames.AnalogB, type, false); MakeColumn(SystemNames.AnalogC, type, false); MakeColumn(SystemNames.AnalogD, type, false); MakeColumn(SystemNames.TempA, type, false); MakeColumn(SystemNames.TempB, type, false); MakeColumn(SystemNames.TempC, type, false); } private DataColumn MakeColumn(string name, Type type, bool state) { DataColumn column = new DataColumn(); column.ColumnName = name; column.DataType = type; column.AutoIncrement = false; column.Caption = name; column.ReadOnly = false; column.Unique = false; _table.Columns.Add(column); _list.Columns[name].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; _list.Columns[name].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; return column; } private void UpdateTable(AnalogSensorData data) { DataRow row = _table.Rows.Find(_recs); if (row == null) { row = _table.NewRow(); row["Index"] = _recs; row["DateTime"] = data.Time; row["Time"] = data.Time.ToLongTimeString(); row[data.SystemName] = data.Eng; _logs = 1; _table.Rows.Add(row); } else { row[data.SystemName] = data.Eng; if (++_logs >= SensorUC.NumberOfActive) { int i = 1; foreach (var item in row.ItemArray) { sheet.Cells[(int)_recs + 3, i++].Value = item; } book.SaveAs(_logFile); _recs++; } } if (!_list.Columns[data.SystemName].HeaderText.Equals(data.SensorName)) { _table.Columns[data.SystemName].Caption = data.SensorName; _list.Columns[data.SystemName].HeaderText = data.SensorName; } _list.FirstDisplayedCell = _list.Rows[_list.Rows.Count - 1].Cells[0]; _list.Update(); } private void UpdateChart(AnalogSensorData data) { if (data.Line.DataSource == null) { if (_chart.InvokeRequired) { _chart.Invoke((MethodInvoker)delegate { _chart.Series.Add(data.Line); data.Line.DataSource = _table; }); } else { _chart.Series.Add(data.Line); data.Line.DataSource = _table; } } if (_chart.InvokeRequired) { _chart.Invoke((MethodInvoker)delegate { data.Line.CheckDataSource(); }); } else { data.Line.CheckDataSource(); } } } }

有人可以帮我解释可能是什么原因吗?或者如何防止它?

Can someone help me what might be the reason? or how to prevent it?

推荐答案

我认为你基于绘图(更新)的代码响应时间并不快。你也在同一个线程中这样做。 要解决,请确保在第一次检查完成后检查,而不是每4秒重新检查一次。如果你的程序是1秒到很晚,它将最终崩溃。如果您尝试检查5小时内的数据,这是有意义的。 尝试每15秒设置一次计时器。你会看到结果(我认为它会在10小时后崩溃)。 I think your response time of the code based on drawing(updating) is not fast enqough. you also do this in the same thread. To solve make sure you check after the first check was done instead of rechecking every 4 seconds. If you program is 1 second to late it will end up in crashing ofcorse. which make sense if you trying to check data from 5 hours. Try to set the timer every 15 seconds. You will see the result (I think it will crash after 10hours).

更多推荐

在C#项目中更新表导致崩溃

本文发布于:2023-11-17 06:22:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609137.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:项目

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!