Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
340
How to update a listview properly from another thread
posted

Good Morning Guys-

I'm just learning C# so please bare with me! I have a winlistview on a tab page that I am populating with a couple hundred (sometimes more) entries. I populate this winlistview when the form loads. Since it takes a bit to search the files and populate that listview, I decided to start a thread to do the work in the background, so the user can watch as the new entries appear.

It doesn't work right. I get "random" errors from the winlistview as it is being populated. Now there are two listviews that are being updated on the main page and its completely random as to which one throws an error and causes the program to crash. The errors also are random. On one launch it complains about one thing, on another launch it'll throw an exception about something else. I am thinking that I am not doing something properly with the threading.

I'm wondering if anyone could point me in the right direction as to what I need to do in order to fire off another thread and have these winlistviews updated "in the background" as the end user watches.

Here's what I've tried (with no success)

On the On Load event, creating a new thread and myThread.Start()'ing that thread. This usually really upsets the apple cart, even the ultralabels will throw errors. So I read that you cannot update the UI from threads, you have to use delegates.

So what I've done is on the load event, create a new thread, call that thread. The thread gets called, and it creates a delegate with the method that populates the winlistview, like this:

 

SimpleDelegate myDelegate = new SimpleDelegate(PopulateListView);

myDelegate();

That does not seem to work for the winlistview either (but the labels now never throw errors). If I use this procedure for a windows listview control, it works fine and updates and never crashes.

So long story short is could someone point me in the right direction for adding items to an ultra winlistview from another thread while the main application is still doing its thing accepting user input and whatnot?

Thanks!!

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
   at Infragistics.Win.UIElement.VerifyChildElements(ControlUIElementBase controlElement, Boolean recursive)

 

 

Parents
  • 45049
    Suggested Answer
    posted

    The short answer is:  never update any Windows Forms control on a thread other than its owning thread.  The kinds of exceptions you've described - especially their "random" nature - are typical of issues caused by unsafe threading.  All Windows Forms controls are not thread-safe, including ours.

    This doesn't mean that you can't use threading.  While you must make changes to the UI on the main thread, you can possibly do other activities on a background thread, which you would then be able to use on the main thread.  For example, you might build your list on a background thread, without that list being bound to WinListView.  When your thread is complete, you can pass that list back to the main thread, and from there bind it to the control.

    I find the following article on MSDN to be a good resource for learning about the risks of threading in Windows Forms, and the variety of ways to use multiple threads safely:
    Safe, Simple Multithreading in Windows Forms

Reply Children