C#:"'System.InvalidOperationException'类型&QUOT的第一个机会异常; [英] C# : "A first chance exception of type 'System.InvalidOperationException'"

查看:501
本文介绍了C#:"'System.InvalidOperationException'类型&QUOT的第一个机会异常;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#类的分配工作,我碰到一个程序崩溃来没有任何错误(除非什么用VS2010编写的调试窗口)。这里是典型的code导致飞机失事的原因:

 公共部分类测试:表
{
    公开测试()
    {
        的InitializeComponent();
    }    私人无效Test_Load(对象发件人,EventArgs的发送)
    {
        的columnHeader头​​;        标题=新的columnHeader();
        header.Text =#;
        header.TextAlign = Horizo​​ntalAlignment.Center;
        header.Width = 30;
        listView1.Columns.Add(头);        TimerCallback TCB = this.UpdateListView;        System.Threading.Timer updateTimer =新System.Threading.Timer(TCB,NULL,0,1000);
    }    公共无效UpdateListView(obj对象)
    {
        ListViewItem的项目;
        listView1.Items.Clear();        的for(int i = 0;我小于10;我++)
        {
            项目=新的ListViewItem(i.ToString());            listView1.Items.Add(项目);
        }    }
}

...我是什么在这里失踪?

** 修改 **

有没有错误,程序只是结束的一样,如果我称之为 System.Environment.Exit(0);

 'System.InvalidOperationException'类型的第一次机会异常出现在System.Windows.Forms.dll中
该方案的[4644] ProgramTest.vshost.exe:托管(v4.0.30319)已退出与code 0(为0x0)。
该方案的[4644] ProgramTest.vshost.exe:程序跟踪已退出与code 0(为0x0)。


解决方案

如果您检查时抛出通用语言运行时异常在休息的时候异常窗口(<大骨节病>控制 + <大骨节病>替代 + <大骨节病>电子在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#:&QUOT;'System.InvalidOperationException'类型&QUOT的第一个机会异常;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆