C# : "A first chance exception of type 'System.InvalidOperationException'"

C#Crash

C# Problem Overview


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).

C# Solutions


Solution 1 - C#

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.

Example of the exceptions window

Solution 2 - C#

The problem here is that your timer starts a thread and when it runs the callback function, the callback function ( updatelistview) is accessing controls on UI thread so this can not be done becuase of this

Solution 3 - C#

Consider using System.Windows.Forms.Timer instead of System.Threading.Timer for a GUI application, for timers that are based on the Windows message queue instead of on dedicated threads or the thread pool.

In your scenario, for the purpose of periodic updates of UI, it seems particularly appropriate since you don't really have a background work or long calculation to perform. You just want to do periodic small tasks that have to happen on the UI thread anyway.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYanick RochonView Question on Stackoverflow
Solution 1 - C#Mark AveniusView Answer on Stackoverflow
Solution 2 - C#user415789View Answer on Stackoverflow
Solution 3 - C#RanView Answer on Stackoverflow