gpt4 book ai didi

c# - 红X GUI 崩溃!我几乎放弃解决它!

转载 作者:太空狗 更新时间:2023-10-29 21:37:07 27 4
gpt4 key购买 nike

我在 MS Visual Studio 2008 C# 中使用 Dundas Charting for Winforms 工具时遇到了一个复杂的错误。

当在图表对象失效时在其上引发 GUI 事件时会发生以下错误。当错误发生时,dundas 图表显示一个大 X 标记。...

************** Exception Text **************

System.ArgumentOutOfRangeException: Axis Object - The Interval can not be zero
Parameter name: diff
at Dundas.Charting.WinControl.AxisScale.a(Double )
at Dundas.Charting.WinControl.Axis.a(Double , Double , AxisScaleSegment , DateTimeIntervalType& )
at Dundas.Charting.WinControl.Axis.a(ChartGraphics , Boolean , AxisScaleSegment , Boolean )
at Dundas.Charting.WinControl.Axis.b(ChartGraphics , Boolean , Boolean )
at Dundas.Charting.WinControl.Axis.Resize(ChartGraphics chartGraph, ElementPosition chartAreaPosition, RectangleF plotArea, Single axesNumber, Boolean autoPlotPosition)
at Dundas.Charting.WinControl.ChartArea.a(ChartGraphics )
at Dundas.Charting.WinControl.ChartPicture.Resize(ChartGraphics chartGraph, Boolean calcAreaPositionOnly)
at Dundas.Charting.WinControl.ChartPicture.Paint(Graphics graph, Boolean paintTopLevelElementOnly, RenderingType renderingType, XmlTextWriter svgTextWriter, Stream flashStream, String documentTitle, Boolean resizable, Boolean preserveAspectRatio)
at Dundas.Charting.WinControl.ChartPicture.Paint(Graphics graph, Boolean paintTopLevelElementOnly)
at Dundas.Charting.WinControl.Chart.OnPaint(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

场景如下:

  • 我有一个 GridView ,其中包含引用正在绘制的系列的对象列表。
  • 图表使用 chart.Invoke(AddData) 每 1 秒更新一次

这是导致崩溃的事件:

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
{
AppDataSeries boundData =
dataGridView1[e.ColumnIndex, e.RowIndex].OwningRow.DataBoundItem as AppDataSeries;

if (boundData.Tag != null)
// Tag is of Type Dundas.Charting.WinControl.Series
{
switch (e.ColumnIndex)
{
case 1:
MUChart.Series[boundData.SeriesName].ChartArea =
boundData.ChartArea.ToString();
// when you change the chart area of a series it
// crashes the chart control
// also when you enable or disable a series using
// series1.Enabled = true,
// it could crash the chart control
MUChart.ChartAreas[boundData.ChartArea].Visible = true;
break;

}
}
}
}

绘图是通过以下方式完成的

后台线程正在捕获

正在引发事件

每秒OnDataAvailable

这是处理程序

void serviceWrapperInstance_DataAvailable(object sender, DataAvailableEventArgs e)
{
if (e.ViewId == currentViewId)
{
if (MUChart.InvokeRequired)
{
MUChart.Invoke((MethodInvoker)AddData);
}

else
{
AddData();
}
}
}
public void AddData()
{
if (MUChart.Series.Count > 0)
{
for (int i = 0; i < currentViewSeries.Count; i++)
{
AddNewPoint(currentViewSeries[i].XValue, MUChart.Series[i],
currentViewSeries[i].YValue * ((currentViewSeries[i].IsInverse) ? -1 : 1),
currentViewSeries[i].ChartColor);

dataSaver[MUChart.Series[i].Name].Add(new DataPoint(currentViewSeries[i].XValue,
(double)currentViewSeries[i].YValue));

}
}
}
public void AddNewPoint(double xValue, Series ptSeries, double yValue,
Color pointColor)
{
try
{
ptSeries.Points.AddXY(xValue, yValue);
if (draggedDroppedSeriesMapper.ContainsKey(ptSeries))
foreach (Series item in draggedDroppedSeriesMapper[ptSeries].DraggedDroppedSeriesVersions)
item.Points.AddXY(xValue, yValue);
MUChart.Invalidate();
// if I remove the previous line the plot doesn’t crash, but doesn’t update !!
}
catch (Exception ex)
{
Logger.Log(TraceLevel.Error, "AddNewPoint()", ex);
}
}

这个错误的有趣之处在于它不会发生在所有机器上。我注意到它发生在高规范的机器上,比如我们的 8 核 CPU DELL 机器,以及我们在这里得到的一台新的四核笔记本电脑。这引起了线程问题的怀疑;然而,线程似乎没问题,因为图表对象是从同一个主线程访问的。

请帮帮我

更新使用发生在函数 dataGridView1_CellEndEdit 中的 setter 的赋值MUChart.Series[boundData.SeriesName].ChartArea= boundData.ChartArea.ToString();在内部调用 chart.invalidate,而更新该图表的调用函数“AddData”会显式调用它。我在 MSDN 库中读到“control.invalidate”不会强制同步绘制,除非在它之后调用 control.update。我几乎可以肯定冲突是在无效时发生的,即使所有事情都发生在同一个线程上,因为重绘是异步发生的。我明白这是怎么回事,但我不知道如何避免。 control.update 对我没有好处。

ChangeTheChartConfigurations();DrawTheChanges() ---->>>> 这是异步工作的更新数据点()DrawTheChanges() ---->>> 这在第一个变化还没有发生时起作用。例如,该系列可能已移动到不同的图表区域,并且 Dundas.Charting.WinControl.AxisScale.a(Double )(堆栈跟踪中的最后一个函数)正在已隐藏的图表区域上被调用。这只是一个想法

更新

我从事件处理程序和 AddNewPoint 函数中记录了线程 ID,它与主线程的相同

最佳答案

与其在绘制每个点后使图表失效,不如绘制所有点然后使图表失效一次。尝试将 MUChart.Invalidate() 从 AddNewPoint() 移动到 AddData() 中的 for 循环之后。

关于c# - 红X GUI 崩溃!我几乎放弃解决它!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2004510/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com