59

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).
Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214

3 Answers3

156

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

Robert MacLean
  • 38,975
  • 25
  • 98
  • 152
Mark Avenius
  • 13,679
  • 6
  • 42
  • 50
  • 1
    no! even with that option no exception thrown and application exited with no break in IDE –  Dec 08 '10 at 22:51
  • 3
    yes! I checked all "Thrown" and I got the error happening where I add the new item (I kinda narrowed that already) and got this answer : "Cross-thread operation not valid: Control 'listView1' accessed from a thread other than the thread it was created on." ... thanks for the shortcut. – Yanick Rochon Dec 08 '10 at 22:52
  • 1
    @Mark Avenius - That helped me with sorting my problem too - never realised that option existed. Very handy. – Vidar Aug 09 '11 at 13:55
  • 1
    A very handy tool! Thanks! – Cary Bondoc Jul 16 '15 at 02:18
  • why don't they set it? – user3290180 Sep 01 '15 at 10:42
  • 1
    @user3290180 By default? If it were always set, the debugger would break on every exception, including ones that the user has already handled (via try/catch blocks.) Usually, I want this setting unset; only in certain circumstances do I care to see exactly when every exception is thrown. – Mark Avenius Sep 01 '15 at 12:56
  • @MarkAvenius really handy. further questions: how can I get such exceptions when it's running as a windows service? I asked because my app sometimes crash "silently" after an uncertain long period of time. I'm now suspecting that there could be some uncaught exceptions there. – foxwendy Sep 22 '15 at 14:36
  • @foxwendy If the service is installed, check your event viewer (I believe the application log, but check system too just in case.) If you're debugging it, this will work just like any other application. – Mark Avenius Sep 22 '15 at 14:42
  • @MarkAvenius so, all I need to do is enable that option in VS and get the project built and when I install it as a windows service, the exceptions will show up in event viewer automatically? Will I get the exceptions caught if I built the project as "Released" ? – foxwendy Sep 22 '15 at 18:50
  • @foxwendy No, this setting has nothing to do with the event viewer. It is only for your debugger in VS. I was just saying that unhandled exceptions show up in event viewer for services that are installed (and pretty much anything that is run outside of the debugger.) – Mark Avenius Sep 22 '15 at 19:13
11

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

Alex
  • 37,502
  • 51
  • 204
  • 332
0

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.

Ran
  • 5,989
  • 1
  • 24
  • 26