ListBox paintListBoxItem function query


#1

I’ve got a bit of a problem that I’m struggling to get around. I’ve got a ThreadWithProgressWindow class which makes a connection to a database and stores all info in an OwnedArray. Easy enough to do, and it works perfectly (as explained in the documentation), however I have a class which uses this OwnedArray to populate it’s ListBox. So basically, when the component isShowing() == true, the ThreadWithProgressWindow class as mentioned above recreates the OwnedArray (to get the latest info), but at the same time the ListBox’s painListBoxItem function wants to draw up the ListBox. This then causes issues as the OwnedArray will probably still be updating.

Does anyone have any ideas?


#2

you need to use a critical section.

OwnedArray<> already inherits from the CriticalSection class, so just declare your instance as a CriticalSection rather than a DummyCriticalSection, and lockArray() / unlockArray() the array object before reading from or writing to it.

There are many different actual implementation models for this kind of thing, and some will be vastly more or less efficient depending on your exact situation, but that’s the basics of it


#3

I did try the lock and unlock function on an OwnedArray with a CriticalSection. However an exception was thrown on some enterCriticalSection function. So what I did was put the painListBoxItems calls which referenced the array in an if block. So once the database starts updating, nothing is painted as the variable is set to false, but once it’s finished it then repaints itself with the new data.


#4

Ouch - multi-threaded programming using “if” statements isn’t how the professionals do it!

I’d say find out what you’re doing that’s causing the exception, and fix it!


#5

Okay, using an if statement was dodgy, and didn’t always work.

The thing that’s giving me an error is that whenever a certain component’s isShowing() method returns true, a call is made to a class which makes a connection to a database and in turn updates the OwnedArray which the component uses to draw listboxes with. I’m getting errors when I view the component as the ListBox tries to draw itself (painListBoxItem) while the database updates the list. So I think I need to pause the paint method until the OwnedArray is finished sorting itself out in the other thread. How would I go about this? Still playing with the locks.


#6

both threads should be locking and unlocking the critical section for that array.

By the way you are wording your problem, it sounds like only the database reading thread is locking the array.


#7

The problem is, that I want to stop the paint method from being called when I change tabs so as to let the database thread update the list, and once that’s done then let the app paint the listboxes.

I thought that the isShowing() function is called before the thing starts painting the listbox, however it doesn’t. It first paints the listboxes (using old OwnedArray) and then the isShowing() event is fired which then updates the OwnedArray, so when the ListBox then updates, the pointer it originally had is out of date.


#8

what about a message manager lock? i haven’t looked into what happens when changing tabs though, that may break it or it may be the solution (as it should stop things like paint getting called)


#9