【问题标题】:Line number not displaying in StackTrace. I can't seem to find the line number an error is happening onStackTrace 中未显示行号。我似乎找不到发生错误的行号
【发布时间】:2021-01-18 23:27:13
【问题描述】:

我有一个应用程序在 try ... catch 块之外引发异常。异常如下所示:

2021-01-18 17:11:38.840 -06:00 [ERR] 集合已修改; 枚举操作可能无法执行。 2021-01-18 17:11:38.848 -06:00 [错误] 在 System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource 资源)在 System.Collections.Generic.List1.Enumerator.MoveNextRare() at System.Collections.Generic.List1.Enumerator.MoveNext() 在 System.Windows.Forms.DataVisualization.Charting.ChartArea.GetPointsInterval(列表`1 seriesList, Boolean isLogarithmic, Double logarithmicBase, Boolean checkSameInterval, Boolean& sameInterval, Series& series) at System.Windows.Forms.DataVisualization.Charting.ChartArea.SetDefaultFromData(轴 轴)在 System.Windows.Forms.DataVisualization.Charting.ChartArea.SetDefaultFromIndexesOrData(轴 轴,轴类型轴类型)在 System.Windows.Forms.DataVisualization.Charting.ChartArea.SetDefaultAxesValues() 在 System.Windows.Forms.DataVisualization.Charting.ChartArea.SetData(布尔 initializeAxes,布尔 checkIndexedAligned) 在 System.Windows.Forms.DataVisualization.Charting.ChartArea.ReCalcInternal() 在 System.Windows.Forms.DataVisualization.Charting.ChartPicture.Paint(图形 图,布尔paintTopLevelElementOnly)在 System.Windows.Forms.DataVisualization.Charting.Chart.OnPaint(PaintEventArgs 吃 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 层)在 System.Windows.Forms.Control.WmPaint(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息& m) 在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

我了解有一个集合在迭代时被修改。我似乎找不到它在哪里。我正在本地网络上的机器上进行远程调试。我已经包裹了我怀疑发生异常的区域, try ... catch 块但没有找到它。

我还在我的 Main() 中添加了以下内容以尝试捕获错误:

        Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

在 main 之后,我有以下处理程序:

    static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
    {
        // Log the exception, display it, etc
        Console.WriteLine(e.Exception.Message);
        Log.Error("");
        Log.Error(e.Exception.Message);
        Log.Error(e.Exception.StackTrace);
    }

    static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        // Log the exception, display it, etc
        Console.WriteLine((e.ExceptionObject as Exception).Message);
        Log.Error("");
        Log.Error((e.ExceptionObject as Exception).Message);
        Log.Error((e.ExceptionObject as Exception).StackTrace);
    }

如何获取引发异常的代码的行号?或者真的......我怎样才能在我的源代码中找到引发错误的地方?

【问题讨论】:

  • 异常中断,然后将堆栈返回到您的代码。
  • 似乎您可能需要锁定该容器,以防止在枚举期间进行修改。

标签: c# exception


【解决方案1】:

查看您的堆栈跟踪,我认为您在试图定位它时正在吠叫电线杆(一棵非常错误的树)。跟踪似乎显示了屏幕重绘逻辑深处某处的崩溃。它正在尝试迭代一个集合并且该集合正在被修改。

您的错误是在不应该修改集合但不会引发异常的情况下尝试修改集合。为此,错误的例程必须同时执行,因此必须在另一个线程中。除了主线程之外,您还有其他东西弄乱了 UI 元素,这是不允许的。处理来自其他线程的 UI 更新是一团糟的例程调用,并且确保您不会踩到某些东西是在后面的皇家痛苦。

【讨论】:

  • 换句话说,您的图表引用了一个集合。仅在 UI 线程中修改该集合,或使用某种并发集合。
  • @JeremyLakeman 我不相信并发集合可以将您从集合已更改的异常中拯救出来。再说了,估计也不是他能控制的。
  • 这是在正确的轨道上。我正在向 MSFT 图表对象中的两个不同数据系列添加一个数据点。我正在修改图表数据,因此出现错误。嗯...看来我需要控制图表的刷新,以防止图表在我更新时迭代数据。
  • @EricSnyder 如果您正在修改,因为它的绘图表明您正在从主线程以外的其他内容进行修改,这本身就是一个问题。使用线程意味着很多麻烦,要确保您没有读取错误的数据,因为它在您读取时发生了更改。
猜你喜欢
  • 2021-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-17
  • 2018-11-14
  • 1970-01-01
相关资源
最近更新 更多