Multithreading with arrays (and control collection issues on the side)

Posted 30 April 2012 - 12:17 AM

…and it all comes back to this pesky little BackgroundWorker thing.

I've hit a bit of a speed bump in my program. I have an array here:

Dim objects(6400) As DBlock

I don't think I need to provide the DBlock code here - it basically inherits Panel. Also I can't (don't have access to it right now).

So what my problem is (if you want to call it that) accessing it from a thread executed by a BackgroundWorker. I understand I can't refer to the forms control collection because it result in an illegal cross-thread call. The forms name is GameScreen.
The code I'd like to be using is this:

For I As Integer = 0 To objects.Count - 1
Me.Controls.Add(objects(I))
Next

The issue is definitely not that my objects are not instantiated. I've made sure that they are.
The error occurs in line 2 of my snippet.
I'd like to know a way to still use a BackgroundWorker but not have it result in an illegal cross-thread call.
So if anyone has any documentation or pointers (0xC6BA!) I'd be extremely grateful.
Right now it takes 81 seconds to instantiate the blocks (that's pretty much what they are - this is a Minecraft style game) and add them within a Sub.
The reason for me wanting to use a BackgroundWorker is to split the workload a bit.
I won't even mention how long it takes to paint them all (at least not in this thread just yet ).

It is a rather large memory hog at least as far as my apps go (80K) and 50 CPU.

Replies To: Multithreading with arrays (and control collection issues on the side)

Re: Multithreading with arrays (and control collection issues on the side)

Posted 30 April 2012 - 10:59 AM

I think the whole design needs a remodel. I think you need a custom class to contain special properties for your tiles. Forget the while panel thing, it's too control heavy. Draw all of then to one canvas based on there properties you draw each one a specific way your mouse down event will handle your coordinates and tile selection if tour use a rectangle as part of the class properties. My 2 cents!

Re: Multithreading with arrays (and control collection issues on the side)

Posted 30 April 2012 - 02:26 PM

Thanks HAWK, I think you've hit a winner.
I probably should have started with that to begin with but I'm not so confident with Graphics just yet - my learning pattern has been quite crap and I only recently touched on Graphics.
I like the idea you have but would it be possible then to replace images with others when the blocks are 'broken' and also save it to a file? My class does store information such as the type and it's accessible through a Public Property. So it's just the question of redesigning the code and also addressing those few issues that I pointed out above and in my original post.
Would your method make the time taken to generate 'terrain' reduce drastically? I have a feeling 81 seconds isn't the best.
Thanks for everything so far!

Re: Multithreading with arrays (and control collection issues on the side)

Posted 30 April 2012 - 09:31 PM

I do believe the graphics would be much more effective. I do some extremely heavy graphic rendering with lightning speeds. Resource images would work well for repeated image use. The trouble with controls is they are heavy on the process thread due to each one has to register it's handle to the OS for the message system i.e. focus, painting, tab order, key press, etc... Where the design I envision uses 1 control a picturebox which has great rendering for your type of work.

If you don't want people to copy that code you should remove it. It's free if they copy it from here.

Re: Multithreading with arrays (and control collection issues on the side)

Posted 01 May 2012 - 09:23 PM

The thing I am having trouble with is the sheer size of the Array you are using.

Here is how I see it;

You have a custom class, in it's construction it builds a List(Of <T>) of poss a smaller class that defines a rectangle, images that it uses, a Boolean for switching b/t 2 images if there are are more than 2 images I would use an enum for the state that object is in. In your paint event for the canvas you pass the graphics object(e.Graphics) to a sub on this custom class where it iterates the List of objects that you are using as tiles for your game. No need to use a BGW, I would just use a simple thread if you find you need it. The great thing about this model is your custom class can be accessed without delegates - just simplifies that part.