Strange form issue.

Anyone ever seen an issue like this

http://weblogs.asp.net/jclarknet/archive/2006/05/02/444845.aspx

It looks like an issue in the framework, as there is no mention of anything in the app itself.



Answer this question

Strange form issue.

  • aspnetgeek

    Do you do this work (clear and re-fill) within a BeginUpdate and EndUpdate call on the list If not, then the framework may try to update the object as soon as you clear it. Shouldn't cause any problems..... but to be safe I like to use the begin/end so layout doesn't occur while you are changing the contents.

    Second, what happens to the information referred to by your ListViewItems Since they are strings do you just let them go away internally, or are the strings part of some larger object that might be affected.

    Finally, when all else fails, maybe it is a framework issue. Can you re-create a short and sweet app that reproduces the problem. Often when you do this you'll find a flaw in your logic, but if not then you have a program that you can submit as a bug report.

    Erik



  • _Igor_

    It looks like you might have a cross threading issue. Are you checking the InvokeRequired flag before making any calls to Invoke or BeginInvoke

    If you're using VS 2005, try this:

    Set the static property Control.CheckForIllegalCrossThreadCalls = true;

    In the Debug menu -> Exceptions, Check the box to Break when a Common Language Runtime Exception is thrown. Then, run your app under the dubugger and see if you get any InvalidOperationExceptions that say Cross-thread operation not valid. If so, there are some UI operations not happening on the UI thread.

    If that isn't it, perhaps a modal dialog could be playing a part also.


  • John Vulner - MSFT

    Recommendation is to call it on the entire update, with the idea being that you say begin, make all your updates, and then end. Typically something like the following:

    BeginUpdate();
    Clear the List;
    foreach (x in SomeCollection)
    {
    Add x to List;
    }
    EndUpdate();

    Let us know how this works.

    Erik



  • KrisLa_MS

    Hi Erik, thanks very much for the reply. I don't have any dispose calls that I make to a ListViewItem at all. The items in the list view are juts strings, I clear the items in the listview then populate the listview thats about the end of the interaction that causes this sporadic issue. I wish i could just get which listview is causing it from the debugger, so far its juts a framework trace.
  • beckerben

    Jason,

    I haven't seen this, but based on the stack trace

    Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
    at System.Windows.Forms.ListViewItemCollection.get_Item(Int32 displayIndex)
    at System.Windows.Forms.ListView.OnHandleDestroyed(EventArgs e)
    at System.Windows.Forms.Control.WmDestroy(Message& m)
    at System.Windows.Forms.Control.WndProc(Message& m)


    I wonder if you might be disposing of a ListViewItem that is still in the ListView. Do your items reference another object, or do you have your own Item class According to the Control documentation, the HandleDestroyed event "Occurs when the control's handle is in the process of being destroyed." This also indicates that the control should still be valid at this point. So the fact that you get a null reference means something might be wrong.

    Could be in .NET as you say, but wouldn't surprise me if this is something you are doing. Do you have any way to track when your item objects are created and destroyed Any special class, reference, or objects used as part of the list Are you using subitems I agree that it doesn't sound like an easy thing to debug.

    Erik




  • Chris R. Timmons

    Hmm, I don't wrap the Clear in a BeginUpdate/EndUpdate block. That may be it, i'll try it. As for the strings, they are just retreived from the call and used in the listview, the framework handles disposing of those.

    So you wrap the clear in a begin/end and adding items to the list

    Cheers.


  • Matt Kosorok

    Another question, related to BeginUpdate. If you are adding the items in a for loop, is it best to call beginupdate prior to the loop and end after the loop Or as each item is actually added to the list.


  • Strange form issue.