C#:“类型为'System.InvalidOperationException'的第一次机会异常”

编程入门 行业动态 更新时间:2024-10-19 14:41:07
本文介绍了C#:“类型为'System.InvalidOperationException'的第一次机会异常”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在C#中的类分配,我遇到了一个程序崩溃,没有任何错误(除了在VS2010的调试窗口中写入)。下面是导致崩溃的典型代码:

public partial class Test:Form { public Test () { InitializeComponent(); } private void Test_Load(object sender,EventArgs e) { ColumnHeader header; header = new ColumnHeader(); header.Text =#; header.TextAlign = Horizo​​ntalAlignment.Center; header.Width = 30; listView1.Columns.Add(header); TimerCallback tcb = this.UpdateListView; System.Threading.Timer updateTimer = new System.Threading.Timer(tcb,null,0,1000); } public void UpdateListView(object obj) { ListViewItem item; listView1.Items.Clear(); for(int i = 0; i <10; i ++) { item = new ListViewItem(i.ToString()); listView1.Items.Add(item); } } }

没有错误,程序只是结束就好像我会调用 System.Environment.Exit(0);

System.Windows.Forms.dll中发生类型为System.InvalidOperationException的第一次机会异常程序'[4644] ProgramTest.vshost.exe :Managed(v4.0.30319)'已退出,代码为0(0x0)。 程序'[4644] ProgramTest.vshost.exe:Program Trace'已退出,代码为0(0x0)。如果您选中 Thrown ( Ctrl + Alt +)中的中断中的 E 在Visual Studio中),那么在调试时抛出异常时,执行应该会中断。

这可能会让您深入了解发生了什么。

Working on a class assignment in C#, I came across a program crash without any error (except what's written in VS2010's debug window). Here is the typical code causing the crash :

public partial class Test : Form { public Test() { InitializeComponent(); } private void Test_Load(object sender, EventArgs e) { ColumnHeader header; header = new ColumnHeader(); header.Text = "#"; header.TextAlign = HorizontalAlignment.Center; header.Width = 30; listView1.Columns.Add(header); TimerCallback tcb = this.UpdateListView; System.Threading.Timer updateTimer = new System.Threading.Timer(tcb, null, 0, 1000); } public void UpdateListView(object obj) { ListViewItem item; listView1.Items.Clear(); for (int i = 0; i < 10; i++) { item = new ListViewItem(i.ToString()); listView1.Items.Add(item); } } }

... what am I missing here?

** EDIT **

There's no error, the program just ends like if I would call System.Environment.Exit(0);

A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll The program '[4644] ProgramTest.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0). The program '[4644] ProgramTest.vshost.exe: Program Trace' has exited with code 0 (0x0).

解决方案

If you check Thrown for Common Language Runtime Exception in the break when an exception window (Ctrl+Alt+E in Visual Studio), then the execution should break while you are debugging when the exception is thrown.

This will probably give you some insight into what is going on.

更多推荐

C#:“类型为'System.InvalidOperationException'的第一次机会异常”

本文发布于:2023-06-03 12:18:56,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   类型   机会   System   InvalidOperationException

发布评论

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

>www.elefans.com

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