6 Replies - 7547 Views - Last Post: 01 May 2012 - 09:23 PM Rate Topic: ***** 1 Votes

#1 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

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 :P).

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

If you need any more info, I'd be happy to provide it. Thanks again!

EDIT: I'll try post the whole code tomorrow. I'm having multiple issues with it.

This post has been edited by DimitriV: 30 April 2012 - 12:23 AM


Is This A Good Question/Topic? 0
  • +

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

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,097
  • Joined: 02-July 08

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!
Was This Post Helpful? 0
  • +
  • -

#3 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

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!

I'll post the code later on when I get to school. Thanks again!

This post has been edited by DimitriV: 30 April 2012 - 02:26 PM

Was This Post Helpful? 0
  • +
  • -

#4 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

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

Posted 30 April 2012 - 03:47 PM

Here's the code for DBlock:
Imports System.ComponentModel
Public Class DBlock

    Inherits System.Windows.Forms.Panel
    Dim btype As String
    Private WithEvents Growth As New Timer
    Private WithEvents Break As New Timer
    Private WithEvents BSequence As New Timer
    Private WithEvents becomegrass As New Timer
    Private WithEvents flow As New Timer
    Private WithEvents check As New Timer

    Private WithEvents gravity As New Timer
    Private WithEvents BANG As New Timer
    Dim hasGrown As Integer
    Dim tType As New Random
    Dim treeType As Integer
    Dim breakLevel As Integer
    ''' <summary>
    ''' Move the block to a certain direction.
    ''' </summary>
    ''' <param name="direction"></param>
    ''' <remarks></remarks>
    Sub moveBLK(ByVal direction As Integer)
        If direction = 0 Then
            Me.Location = New Point(Me.Location.X - 5, Me.Location.Y)
        End If
        If direction = 1 Then
            Me.Location = New Point(Me.Location.X + 5, Me.Location.Y)
        End If
    End Sub
    Sub checkg(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub
    Public Sub New(ByVal type As String, ByVal x As Integer, ByVal y As Integer, ByVal img As Image)
        InitializeComponent()
        Break.Interval = 1000
        MakeBlock(type, x, y, img)

    End Sub
    Sub MakeBlock(ByVal blocktype As String, ByVal locx As Integer, ByVal locy As Integer, ByVal img As Image)
        Try
            Break.Stop()

            flow.Stop()
            BSequence.Stop()

            breakLevel = 0
            Me.Size = New Size(32, 32)
            Me.Location = New Point(locx, locy)
            Me.BackgroundImage = img
            btype = blocktype
            If blocktype = "sapling" Then
                grow()

            ElseIf blocktype = "dirt" Then
                For Each Control In GameScreen.Controls
                    If TypeOf Control Is DBlock Then
                        If Control.location = New Point(Me.Location.X, Me.Location.Y - 32) Then
                            If Control.BlockType = "air" Or Control.BlockType = "bluebell" Or Control.BlockType = "rose" Or Control.BlockType = "dandelion" Or Control.BlockType = "sapling" Then
                                becomegrass.Interval = 5000
                                becomegrass.Start()
                            End If
                        End If
                    End If
                Next
            ElseIf blocktype = "water" Then
                flow.Interval = 500
                flow.Start()
            ElseIf blocktype = "tnt" Then
                BANG.Interval = 1000
                BANG.Start()

            End If
            If blocktype = "dirt" Or blocktype = "sand" Or blocktype = "grassb" Then
                gravity.Interval = 600
                gravity.Start()

            End If




            With Me
                AddHandler .MouseDown, AddressOf blockclick
                AddHandler .MouseUp, AddressOf bup
                AddHandler .Paint, AddressOf painn
            End With
        Catch ex As Exception

        End Try
        GameScreen.Controls.Add(Me)
    End Sub
    Sub painn(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
        ' Dim brt As Single = 50
        ' Dim image_attr As New Imaging.ImageAttributes
        ' Dim cm As Imaging.ColorMatrix = New Imaging.ColorMatrix(New Single()() _
        '    { _
        '    New Single() {1.0, 0.0, 0.2, 0.0, 1.0}, _
        '    New Single() {0.0, 0.0, 0.2, 0.0, 1.0}, _
        '    New Single() {0.0, 0.0, 1.2, 0.0, 1.0}, _
        '    New Single() {0.0, 0.0, 0.2, 1.0, 1.0}, _
        '   New Single() {0.0, 0.0, 0.2, 0.0, 1.0}})
        '
        'Dim rect As Rectangle = _
        'Rectangle.Round(Me.BackgroundImage.GetBounds(GraphicsUnit.Pixel))
        'Dim wid As Integer = Me.BackgroundImage.Width
        'Dim hgt As Integer = Me.BackgroundImage.Height
        '
        'image_attr.SetColorMatrix(cm)
        'e.Graphics.DrawImage(Me.BackgroundImage, rect, 0, 0, wid, _
        '    hgt, GraphicsUnit.Pixel, image_attr)

    End Sub
    Sub gtick(ByVal sender As Object, ByVal e As EventArgs) Handles gravity.Tick
        For Each b In GameScreen.Controls
            If TypeOf b Is DBlock Then
                If b.BlockType = "air" Or b.BlockType = "water" Then
                    If b.Location = New Point(Me.Location.X, Me.Location.Y + 32) Then
                        Growth.Start()
                        b.MakeBlock(btype, b.location.x, b.location.y, Me.BackgroundImage)
                        MakeBlock("air", Me.Location.X, Me.Location.Y, My.Resources.air)
                        gravity.Stop()

                    End If
                End If

            End If
        Next
    End Sub
    Sub flowing(ByVal sender As Object, ByVal e As EventArgs) Handles flow.Tick
        For Each b In GameScreen.Controls
            If TypeOf b Is DBlock Then
                If b.BlockType = "air" Then
                    If b.Location = New Point(Me.Location.X - 32, Me.Location.Y) Or b.Location = New Point(Me.Location.X + 32, Me.Location.Y) Or b.Location = New Point(Me.Location.X, Me.Location.Y + 32) Then
                        b.MakeBlock("water", b.location.x, b.location.y, My.Resources.water)
                    End If
                End If

            End If
        Next
    End Sub
    Sub tntexplosion(ByVal sender As Object, ByVal e As System.EventArgs) Handles BANG.Tick
        Dim radius As New Rectangle(Me.Location.X - 64, Me.Location.Y - 64, 32 * 5, 32 * 5)
        For Each Control In GameScreen.Controls
            If TypeOf Control Is DBlock Then
                If New Rectangle(Control.location.x, Control.location.y, Control.width, Control.height).IntersectsWith(radius) Then
                    If Not Control.BlockType = "bedrock" Then
                        Control.makeblock("air", Control.location.x, Control.location.y, My.Resources.air)
                    End If

                End If
            End If
        Next
        MakeBlock("air", Me.Location.X, Me.Location.Y, My.Resources.air)
        BANG.Stop()
    End Sub
    Sub bgrass(ByVal sender As Object, ByVal e As EventArgs) Handles becomegrass.Tick
        becomegrass.Stop()
        Me.btype = "grassb"
        Me.BackgroundImage = My.Resources.grassb
    End Sub
    Sub bup(ByVal sender As Object, ByVal e As MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Break.Stop()

            BSequence.Stop()
        End If
    End Sub
    Sub BSequence_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles BSequence.Tick
        Dim bimage As Bitmap = New Bitmap(Me.BackgroundImage)
        Dim g As Graphics = Graphics.FromImage(bimage)


        If breakLevel = 1 Then

            g.DrawImage(My.Resources.BreakSeries1, 0, 0, 32, 32)
            Me.BackgroundImage = bimage
        ElseIf breakLevel = 2 Then

            g.DrawImage(My.Resources.BreakSeries2, 0, 0, 32, 32)
            Me.BackgroundImage = bimage
        ElseIf breakLevel = 3 Then

            g.DrawImage(My.Resources.BreakSeries3, 0, 0, 32, 32)
            Me.BackgroundImage = bimage
        ElseIf breakLevel = 4 Then

            g.DrawImage(My.Resources.BreakSeries4, 0, 0, 32, 32)
            Me.BackgroundImage = bimage
        ElseIf breakLevel = 5 Then

            g.DrawImage(My.Resources.BreakSeries5, 0, 0, 32, 32)
            Me.BackgroundImage = bimage
        ElseIf breakLevel = 6 Then


            Me.BackgroundImage = My.Resources.air
            btype = "air"
            Break.Stop()
            Exit Sub

        End If
        breakLevel += 1
    End Sub

    Sub grow()
        Growth.Interval = 1500
        Growth.Start()
        treeType = tType.Next(0, 3)





    End Sub
    Sub p_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Growth.Tick
        If treeType = 0 Then
            If hasGrown = 1 Then
                btype = "deadbush"
                Growth.Stop()
                hasGrown = 0
            End If

        ElseIf treeType = 1 Then

            If hasGrown = 2 Then
                Me.BlockType = "woood"
                Me.BackgroundImage = My.Resources.wood
            ElseIf hasGrown = 6 Then

                BlockType = "wood"
                Me.BackgroundImage = My.Resources.wood
                For Each control In GameScreen.Controls
                    If TypeOf control Is DBlock Then

                        If CType(control, DBlock).Location = (New Point(Me.Location.X, Me.Location.Y - 32)) Then
                            CType(control, DBlock).MakeBlock("wood", CType(control, DBlock).Location.X, CType(control, DBlock).Location.Y, My.Resources.wood)

                        End If
                    End If
                Next
            ElseIf hasGrown = 11 Then
                For Each control In GameScreen.Controls
                    If TypeOf control Is DBlock Then

                        If CType(control, DBlock).Location = (New Point(Me.Location.X, Me.Location.Y - 64)) Then
                            CType(control, DBlock).MakeBlock("leaves", CType(control, DBlock).Location.X, CType(control, DBlock).Location.Y, My.Resources.leaves)

                        End If
                    End If
                Next
            ElseIf hasGrown = 15 Then

                For Each control In GameScreen.Controls
                    If TypeOf control Is DBlock Then

                        If CType(control, DBlock).Location = (New Point(Me.Location.X, Me.Location.Y - 64)) Or CType(control, DBlock).Location = (New Point(Me.Location.X - 32, Me.Location.Y - 32)) Or CType(control, DBlock).Location = (New Point(Me.Location.X + 32, Me.Location.Y - 32)) Then
                            CType(control, DBlock).MakeBlock("leaves", CType(control, DBlock).Location.X, CType(control, DBlock).Location.Y, My.Resources.leaves)

                        End If
                    End If
                Next
                Growth.Stop()
                hasGrown = 0
            End If
        ElseIf treeType = 2 Then
            If hasGrown = 2 Then
                Me.BlockType = "wood"
                Me.BackgroundImage = My.Resources.wood
            ElseIf hasGrown = 6 Then

                BlockType = "wood"
                Me.BackgroundImage = My.Resources.wood
                For Each control In GameScreen.Controls
                    If TypeOf control Is DBlock Then

                        If CType(control, DBlock).Location = (New Point(Me.Location.X, Me.Location.Y - 32)) Then
                            CType(control, DBlock).MakeBlock("wood", CType(control, DBlock).Location.X, CType(control, DBlock).Location.Y, My.Resources.wood)

                        End If
                        If CType(control, DBlock).Location = (New Point(Me.Location.X, Me.Location.Y - 64)) Then
                            CType(control, DBlock).MakeBlock("leaves", CType(control, DBlock).Location.X, CType(control, DBlock).Location.Y, My.Resources.leaves)

                        End If
                    End If
                Next
            ElseIf hasGrown = 11 Then
                For Each control In GameScreen.Controls
                    If TypeOf control Is DBlock Then

                        If CType(control, DBlock).Location = (New Point(Me.Location.X, Me.Location.Y - 96)) Or CType(control, DBlock).Location = (New Point(Me.Location.X - 32, Me.Location.Y - 96)) Then
                            CType(control, DBlock).MakeBlock("leaves", CType(control, DBlock).Location.X, CType(control, DBlock).Location.Y, My.Resources.leaves)

                        End If
                    End If
                Next
            ElseIf hasGrown = 15 Then

                For Each control In GameScreen.Controls
                    If TypeOf control Is DBlock Then

                        If CType(control, DBlock).Location = (New Point(Me.Location.X, Me.Location.Y - 64)) Or CType(control, DBlock).Location = (New Point(Me.Location.X - 32, Me.Location.Y - 32)) Or CType(control, DBlock).Location = (New Point(Me.Location.X + 32, Me.Location.Y - 32)) Then
                            CType(control, DBlock).MakeBlock("leaves", CType(control, DBlock).Location.X, CType(control, DBlock).Location.Y, My.Resources.leaves)

                        End If
                    End If
                Next
                Growth.Stop()
                hasGrown = 0
            End If
        End If


        hasGrown += 1
    End Sub
    Sub blockclick(ByVal sender As Object, ByVal e As MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            If btype = "water" Then

            ElseIf btype = "air" Then

                MakeBlock(GameScreen.btype, Me.Location.X, Me.Location.Y, GameScreen.block)
            ElseIf btype = "bedrock" Then

            ElseIf btype = "lava" Then

            ElseIf btype = "dirt" Then
                Break.Interval = 200
            ElseIf btype = "diamondore" Then
                Break.Interval = 3000
            ElseIf btype = "leaves" Then
                Break.Interval = 100
            ElseIf btype = "grassb" Then
                Break.Interval = 200
            ElseIf btype = "woodplanks" Then
                Break.Interval = 500
            ElseIf btype = "ironore" Then
                Break.Interval = 800
            ElseIf btype = "stone" Then
                Break.Interval = 800
            ElseIf btype = "sapling" Then
                Me.btype = "air"
                Me.BackgroundImage = My.Resources.air
            ElseIf btype = "deadbush" Then
                Break.Interval = 225
            ElseIf btype = "sand" Then
                Break.Interval = 175
            ElseIf btype = "brick" Then
                Break.Interval = 1500
            ElseIf btype = "bluebell" Or btype = "dandelion" Or btype = "rose" Then
                Me.btype = "air"
                Me.BackgroundImage = My.Resources.air

            ElseIf btype = "goldore" Then
                Break.Interval = 1500
            ElseIf btype = "tnt" Then
                Break.Interval = 400
            ElseIf btype = "coalore" Then
                Break.Interval = 900
            ElseIf btype = "wood" Then
                Break.Interval = 700
                If Growth.Enabled = True Then
                    Growth.Stop()
                    hasGrown = 0
                End If
            Else
                Break.Interval = 100


                BSequence.Interval = Break.Interval / 7
                BSequence.Start()
                Break.Start()


            End If
        End If



    End Sub
    Public Property BlockType() As String
        Get
            Return btype
        End Get
        Set(ByVal value As String)
            btype = value
        End Set
    End Property
    Public Sub Breaking(ByVal sender As Object, ByVal e As System.EventArgs) Handles Break.Tick
        MakeBlock("air", Me.Location.X, Me.Location.Y, My.Resources.air)
        BSequence.Stop()
        breakLevel = 0

    End Sub
    ''' <summary>
    ''' Return all of the information about this block in a string.
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function ToString() As String
        Return Me.Location.X.ToString + ";" + Me.Location.Y.ToString + ";" + btype

    End Function
End Class



And here is the main GameScreen code:
Public Class GameScreen
    Dim placeb As Image
    Dim objects(6400) As DBlock
    Dim b1, b2, b3, b4, b5, b6, b7, b8, b9 As String
    Dim stype As String = "stone"
    Dim anything As String


    Private Sub GameScreen_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyData = Keys.D Then
            CamLeft.Start()

        End If
        If e.KeyData = Keys.A Then
            CamRight.Start()

        End If
    End Sub

    Private Sub GameScreen_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyData = Keys.D Then
            CamLeft.Stop()

        End If
        If e.KeyData = Keys.A Then
            CamRight.Stop()

        End If
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Panel1.Location = New Point(Me.Width / 2 - Panel1.Width / 2, Panel1.Location.Y)

        generate()
        b1 = "stone"
        b2 = "tnt"
        b3 = "dirt"
        b4 = "woodplanks"
        b5 = "wood"
        b6 = "brick"
        b7 = "rose"
        b8 = "bluebell"
        b9 = "dandelion"
    End Sub
    Sub generate()

        Dim genString As String
        genString = String.Empty
        Dim SurfaceRandom As Integer
        Dim GroundRandom As Integer
        Dim TLRandom As Integer
        Dim underGround As Integer
        Dim sapRandom As Integer
        Dim trRnd As New Random()
        Dim ugRnd As New Random()
        Dim tlrand As New Random()
        Dim srandom As New Random()
        Dim grandom As New Random()
        Dim spRnd As New Random()
        StartTemp.BlockProgressBar1.progress = "Building Terrain..."
        For blockCount As Integer = 1 To 6400
            Dim rnd As New Random()

            'SKY LOOP
            If blockCount < 128 * 50 / 2 Then
                genString += "air/"
            ElseIf blockCount < 128 * 51 / 2 Then 'SAPLING LOOP
                sapRandom = spRnd.Next(0, 7)
                If sapRandom = 0 Then
                    genString += "air/"
                ElseIf sapRandom = 1 Then
                    genString += "air/"
                ElseIf sapRandom = 2 Then
                    If genString.EndsWith("sapling/") Then
                        blockCount += 1
                        genString += "air/"
                    Else
                        genString += "sapling/"
                    End If

                ElseIf sapRandom = 3 Then
                    genString += "air/"
                ElseIf sapRandom = 4 Then
                    If genString.EndsWith("bluebell/") Then
                        blockCount += 1
                        genString += "air/"
                    Else
                        genString += "bluebell/"
                    End If

                ElseIf sapRandom = 5 Then
                    If genString.EndsWith("dandelion/") Then
                        blockCount += 1
                        genString += "air/"
                    Else
                        genString += "dandelion/"
                    End If


                ElseIf sapRandom = 6 Then
                    If genString.EndsWith("rose/") Then
                        blockCount += 1
                        genString += "air/"
                    Else
                        genString += "rose/"
                    End If
                End If
            ElseIf blockCount < 128 * 52 / 2 Then 'TOP LEVEL LOOP
                TLRandom = tlrand.Next(0, 5)
                If TLRandom = 0 Then
                    genString += "grassb/"
                ElseIf TLRandom = 1 Then
                    genString += "grassb/"
                ElseIf TLRandom = 2 Then
                    genString += "grassb/"
                ElseIf TLRandom = 3 Then
                    genString += "grassb/"
                ElseIf TLRandom = 4 Then
                    genString += "water/"
                ElseIf TLRandom = 5 Then
                    genString += "grassb/"
                End If
            ElseIf blockCount < 128 * 67 / 2 Then 'SURFACE UNDERGROUND LOOP
                SurfaceRandom = srandom.Next(0, 4)
                If SurfaceRandom = 0 Then
                    genString += "dirt/"
                ElseIf SurfaceRandom = 1 Then
                    genString += "water/"
                ElseIf SurfaceRandom = 2 Then
                    genString += "sand/"
                ElseIf SurfaceRandom = 3 Then
                    genString += "dirt/"
                End If
            ElseIf blockCount < 128 * 89 / 2 Then 'SURFACE UNDERGROUND LOOP
                SurfaceRandom = srandom.Next(0, 4)
                If SurfaceRandom = 0 Then
                    genString += "dirt/"
                ElseIf SurfaceRandom = 1 Then
                    genString += "dirt/"
                ElseIf SurfaceRandom = 2 Then
                    genString += "sand/"
                ElseIf SurfaceRandom = 3 Then
                    genString += "dirt/"
                End If

            ElseIf blockCount < 128 * 94 / 2 Then 'DIRT-STONE TRANSITION LAYER
                Dim transitionRandom As Integer = trRnd.Next(0, 4)
                If transitionRandom = 0 Then
                    genString += "dirt/"
                ElseIf transitionRandom = 1 Then
                    genString += "stone/"
                ElseIf transitionRandom = 2 Then
                    genString += "dirt/"
                ElseIf transitionRandom = 3 Then
                    genString += "stone/"
                End If
            ElseIf blockCount < 128 * 119 / 2 Then 'STONE AND ORE LAYER
                GroundRandom = grandom.Next(0, 13)
                If GroundRandom = 0 Then
                    genString += "coalore/"
                ElseIf GroundRandom = 1 Then
                    genString += "stone/"
                ElseIf GroundRandom = 2 Then
                    genString += "stone/"
                ElseIf GroundRandom = 3 Then
                    genString += "stone/"
                ElseIf GroundRandom = 4 Then
                    genString += "coalore/"
                ElseIf GroundRandom = 5 Then
                    genString += "stone/"
                ElseIf GroundRandom = 6 Then
                    genString += "stone/"
                ElseIf GroundRandom = 7 Then
                    genString += "stone/"
                ElseIf GroundRandom = 8 Then
                    genString += "stone/"
                ElseIf GroundRandom = 9 Then
                    genString += "colaore/"
                ElseIf GroundRandom = 10 Then
                    genString += "stone/"
                ElseIf GroundRandom = 11 Then
                    genString += "ironore/"
                ElseIf GroundRandom = 12 Then
                    genString += "goldore/"

                End If
            ElseIf blockCount < 128 * 127 / 2 Then 'BEDROCK AND STONE TRANSITION LAYER
                underGround = ugRnd.Next(0, 6)
                If underGround = 0 Then
                    genString += "bedrock/"
                ElseIf underGround = 1 Then
                    genString += "stone/"
                ElseIf underGround = 2 Then
                    genString += "stone/"
                ElseIf underGround = 3 Then
                    genString += "diamondore/"
                ElseIf underGround = 4 Then
                    genString += "bedrock/"
                ElseIf underGround = 5 Then
                    genString += "lava/"
                End If
            Else 'LOWEST BEDROCK LAYER

                genString += "bedrock/"
            End If
        Next
        renderTerrain(genString)
        StartTemp.Hide()

    End Sub
    ''' <summary>
    ''' Render the terrain - pass an array to the subprocedure.
    ''' </summary>
    ''' <param name="s"></param>
    ''' <remarks></remarks>
    Sub renderTerrain(ByVal s As String)
        Dim stopw As Stopwatch = New Stopwatch
        stopw.Start()
        Dim p As String() = s.Split("/")
        Dim renderingBlock As Long = 0
        Dim xAxisRender As Integer
        Dim yAxisRender As Integer
        Do Until renderingBlock = p.Length - 1

            If renderingBlock Mod 128 = 0 Then
                xAxisRender = 0
                yAxisRender += 1
            End If
            Try
                If p(renderingBlock) = "air" Then
                    Dim b As New DBlock("air", xAxisRender * 32, yAxisRender * 32, My.Resources.air)

                ElseIf p(renderingBlock) = "stone" Then
                    Dim b As New DBlock("stone", xAxisRender * 32, yAxisRender * 32, My.Resources.stone)

                ElseIf p(renderingBlock) = "grassb" Then
                    Dim b As New DBlock("grassb", xAxisRender * 32, yAxisRender * 32, My.Resources.grassb)

                ElseIf p(renderingBlock) = "dirt" Then
                    Dim b As New DBlock("dirt", xAxisRender * 32, yAxisRender * 32, My.Resources.dirt)

                ElseIf p(renderingBlock) = "stone" Then
                    Dim b As New DBlock("stone", xAxisRender * 32, yAxisRender * 32, My.Resources.stone)

                ElseIf p(renderingBlock) = "sapling" Then
                    Dim b As New DBlock("sapling", xAxisRender * 32, yAxisRender * 32, My.Resources.sapling2)

                ElseIf p(renderingBlock) = "bedrock" Then
                    Dim b As New DBlock("bedrock", xAxisRender * 32, yAxisRender * 32, My.Resources.bedrock)

                ElseIf p(renderingBlock) = "water" Then
                    Dim b As New DBlock("water", xAxisRender * 32, yAxisRender * 32, My.Resources.water)

                ElseIf p(renderingBlock) = "ironore" Then
                    Dim b As New DBlock("ironore", xAxisRender * 32, yAxisRender * 32, My.Resources.ironore)

                ElseIf p(renderingBlock) = "sand" Then
                    Dim b As New DBlock("sand", xAxisRender * 32, yAxisRender * 32, My.Resources.sand)

                ElseIf p(renderingBlock) = "diamondore" Then
                    Dim b As New DBlock("diamondore", xAxisRender * 32, yAxisRender * 32, My.Resources.diamondore)

                ElseIf p(renderingBlock) = "lava" Then
                    Dim b As New DBlock("lava", xAxisRender * 32, yAxisRender * 32, My.Resources.lava)

                ElseIf p(renderingBlock) = "dandelion" Then
                    Dim b As New DBlock("dandelion", xAxisRender * 32, yAxisRender * 32, My.Resources.dandelion)

                ElseIf p(renderingBlock) = "bluebell" Then
                    Dim b As New DBlock("bluebell", xAxisRender * 32, yAxisRender * 32, My.Resources.bluebell)

                ElseIf p(renderingBlock) = "rose" Then
                    Dim b As New DBlock("rose", xAxisRender * 32, yAxisRender * 32, My.Resources.rose)

                ElseIf p(renderingBlock) = "coalore" Then
                    Dim b As New DBlock("coalore", xAxisRender * 32, yAxisRender * 32, My.Resources.coalore)

                ElseIf p(renderingBlock) = "goldore" Then
                    Dim b As New DBlock("goldore", xAxisRender * 32, yAxisRender * 32, My.Resources.goldore)

                End If
                renderingBlock += 1
                xAxisRender += 1
                Debug.WriteLine("block" & renderingBlock.ToString & p(renderingBlock))
                StartTemp.BlockProgressBar1.Value += 128 / 6400
                StartTemp.BlockProgressBar1.progress = "Rendering Terrain..."
            Catch
            End Try

        Loop
        StartTemp.BlockProgressBar1.progress = "Finalizing..."

        stopw.Stop()

       

    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        IO.File.WriteAllText("world123.txt", "")
        Dim se As New IO.StreamWriter("world123.txt", True)
        For Each pnl In Me.Controls
            If TypeOf pnl Is DBlock Then

                se.WriteLine(pnl.Location.X & ";" & pnl.Location.Y & ";" & pnl.BlockType)

            End If

        Next
        MsgBox("done")
        se.Close()
    End Sub

    Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
        MsgBox(Me.Height.ToString)

    End Sub

    Private Sub CamLeft_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CamLeft.Tick
        For Each Control In Me.Controls
            If TypeOf Control Is DBlock Then
                Control.moveBLK(0)
            End If
        Next
    End Sub

    Private Sub CamRight_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CamRight.Tick
        For Each Control In Me.Controls
            If TypeOf Control Is DBlock Then
                Control.moveBLK(1)

            End If
        Next
    End Sub

    Private Sub hb9_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles hb9.Click
        placeb = CType(sender, Panel).BackgroundImage
        stype = b9

    End Sub
    Private Sub hb8_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles hb8.Click
        placeb = CType(sender, Panel).BackgroundImage
        stype = b8

    End Sub
    Private Sub hb7_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles hb7.Click
        placeb = CType(sender, Panel).BackgroundImage
        stype = b7

    End Sub
    Private Sub hb6_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles hb6.Click
        placeb = CType(sender, Panel).BackgroundImage
        stype = b6

    End Sub
    Private Sub hb5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles hb5.Click
        placeb = CType(sender, Panel).BackgroundImage
        stype = b5

    End Sub
    Private Sub hb4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles hb4.Click
        placeb = CType(sender, Panel).BackgroundImage
        stype = b4

    End Sub
    Private Sub hb3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles hb3.Click
        placeb = CType(sender, Panel).BackgroundImage
        stype = b3


    End Sub
    Private Sub hb2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles hb2.Click
        placeb = CType(sender, Panel).BackgroundImage
        stype = b2

    End Sub
    Private Sub hb1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles hb1.Click
        placeb = CType(sender, Panel).BackgroundImage
        stype = b1

    End Sub
    Private Sub hb9_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles hb9.Paint

    End Sub
    Public Property block() As Image
        Get
            Return placeb
        End Get
        Set(ByVal value As Image)

        End Set
    End Property
    Public ReadOnly Property btype() As String
        Get
            Return stype
        End Get
     
    End Property


    Private Sub hb1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles hb1.Paint

    End Sub

   
    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted

    End Sub
End Class


I understand it may be a bit of a mess - I'm still going at it. But for anyone viewing this thread, please don't keep this code. I put a lot of work into it and I don't want it copied. Thanks!
Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,097
  • Joined: 02-July 08

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.
Was This Post Helpful? 0
  • +
  • -

#6 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

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

Posted 30 April 2012 - 10:41 PM

Thanks. How do you suppose that I implement such an idea? It seems pretty difficult to me. How would you go about it?
Was This Post Helpful? 0
  • +
  • -

#7 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,097
  • Joined: 02-July 08

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1