Visualbasic multithreading help

Discussion in 'Mixed Languages' started by stevemk14ebr, Jul 9, 2011.

  1. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    My code gives me the following error:"Cross-thread operation not valid: Control 'Frmmain' accessed from a thread other than the thread it was created on."

    Code: Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles Worker1.DoWork
    Dim looppoint As Point
    For loop1 = 0 To 683
    For loop2 = 0 To 779
    looppoint.X = loop1
    looppoint.Y = loop2
    If board(looppoint.X, looppoint.Y) = 0 Then

    Dim shape As New OvalShape
    canvas.Parent = Me
    shape.Parent = canvas
    shape.BackStyle = BackStyle.Opaque
    shape.BackColor = Color.Black
    shape.Size = New System.Drawing.Size(1, 1)
    shape.Location = New System.Drawing.Point(looppoint.X, looppoint.Y)
    shape.Enabled = False
    End If
    Next
    Next
    End Sub

    I need help on how i would be able to get this to work using the background worker tool or at least running that code on a sepearate thread from the rest of my app any help apreciated a.s.a.p been working on this app for a month and want it done thanks :D


    same question by me on this site:
    http://answers.yahoo.com/question/index?qid=20110708164251AATRw0I
     
  2. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,218
    22,277
    210
    Cant gather much from that without seeing the rest of your code o_O
     
  3. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    #3 stevemk14ebr, Jul 9, 2011
    Last edited: Jul 9, 2011
    (OP)
    here is the array: Dim board(683, 779) As Integer

    runs this on load:
    (many for loops setting quantities of array)
    Worker1.RunWorkerAsync()


    then :
    a sub for movement timer
    and finaly the backgroundworker sub

    what the background worker is supposed to do is loop through my array on a separate thread and if a point in the array is = to 0 then creat a black oval at that point on the screen as specified in the array

    i know the loop works on a single thread but it gives an error while in the backgroundworker sub/2nd thread
     
  4. BobSheep

    BobSheep MDL Guru

    Apr 19, 2010
    2,330
    1,377
    90
    Please post your code.
     
  5. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    Imports Microsoft.VisualBasic.PowerPacks
    Imports System.Threading.Tasks
    Public Class Frmmain
    Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Integer
    Delegate Sub drawdot()
    Dim board(683, 779) As Integer
    Dim canvas As New ShapeContainer
    'drawing takes a while to load and cant move pacman
    Dim draw As Boolean = True
    Dim testmode As Boolean = True
    Dim upd, downd, leftd, rightd As Boolean
    Private Sub Frmmain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'setting open and closed spaces for moving pacman in a 2d array
    'borders
    For loop1 = 0 To 683
    For loop2 = 0 To 779
    board(loop1, loop2) = 1
    Next
    Next
    For loop3 = 17 To 648
    For loop4 = 117 To 158
    board(loop3, loop4) = 0
    Next
    Next
    For loop5 = 17 To 62
    For loop6 = 23 To 230
    board(loop5, loop6) = 0
    Next
    Next
    For loop7 = 17 To 318
    For loop8 = 23 To 64
    board(loop7, loop8) = 0
    Next
    Next
    For loop9 = 134 To 176
    For loop10 = 64 To 117
    board(loop9, loop10) = 0
    Next
    Next
    For loop11 = 277 To 318
    For loop12 = 64 To 117
    board(loop11, loop12) = 0
    Next
    Next
    For loop13 = 347 To 388
    For loop14 = 64 To 117
    board(loop13, loop14) = 0
    Next
    Next
    For loop15 = 348 To 648
    For loop16 = 23 To 64
    board(loop15, loop16) = 0
    Next
    Next
    For loop17 = 488 To 532
    For loop18 = 64 To 117
    board(loop17, loop18) = 0
    Next
    Next
    For loop19 = 606 To 648
    For loop20 = 64 To 117
    board(loop19, loop20) = 0
    Next
    Next
    For loop21 = 62 To 178
    For loop22 = 187 To 230
    board(loop21, loop22) = 0
    Next
    Next
    For loop23 = 132 To 178
    For loop24 = 158 To 187
    board(loop23, loop24) = 0
    Next
    Next
    For loop25 = 205 To 248
    For loop26 = 158 To 231
    board(loop25, loop26) = 0
    Next
    Next
    For loop27 = 248 To 320
    For loop28 = 186 To 231
    board(loop27, loop28) = 0
    Next
    Next
    For loop29 = 348 To 417
    For loop30 = 186 To 231
    board(loop29, loop30) = 0
    Next
    Next
    For loop31 = 417 To 461
    For loop32 = 158 To 231
    board(loop31, loop32) = 0
    Next
    Next
    For loop33 = 132 To 178
    For loop34 = 230 To 656
    board(loop33, loop34) = 0
    Next
    Next
    For loop35 = 204 To 462
    For loop36 = 256 To 298
    board(loop35, loop36) = 0
    Next
    Next
    For loop37 = 274 To 321
    For loop38 = 231 To 256
    board(loop37, loop38) = 0
    Next
    Next
    For loop39 = 348 To 392
    For loop40 = 231 To 256
    board(loop39, loop40) = 0
    Next
    Next
    For loop41 = 1 To 248
    For loop42 = 328 To 372
    board(loop41, loop42) = 0
    Next
    Next
    For loop43 = 418 To 663
    For loop44 = 328 To 372
    board(loop43, loop44) = 0
    Next
    Next
    'draws a dot where board =1
    Worker1.RunWorkerAsync()


    End Sub


    Private Sub movement_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles movement.Tick
    movement.Interval = 1
    Dim point1 As New Point(pacface.Left, pacface.Top)
    Dim point2 As New Point(pacface.Right, pacface.Top)
    Dim point3 As New Point(pacface.Left, pacface.Bottom)
    Dim point4 As New Point(pacface.Right, pacface.Bottom)
    '5,6,7,8 for the middle of pacman for the smaller blocks
    Dim point5 As New Point(pacface.Left, pacface.Top + (pacface.Height / 2))
    Dim point6 As New Point(pacface.Right, pacface.Top + (pacface.Height / 2))
    Dim point7 As New Point(pacface.Left + (pacface.Width / 2), pacface.Top)
    Dim point8 As New Point(pacface.Left + (pacface.Width / 2), pacface.Bottom)
    If pacface.Bounds.IntersectsWith(Leftpass.Bounds) Then
    pacface.Location = New Point(x:=605, y:=332)
    End If
    If pacface.Bounds.IntersectsWith(rightpass.Bounds) Then
    pacface.Location = New Point(x:=16, y:=332)
    End If
    If GetAsyncKeyState(Keys.Right) And board(point1.X + 1, point1.Y) = 0 And board(point2.X + 1, point2.Y) = 0 And board(point3.X + 1, point3.Y) = 0 And board(point4.X + 1, point4.Y) = 0 And board(point5.X + 1, point5.Y) = 0 And board(point6.X + 1, point6.Y) = 0 And board(point7.X + 1, point7.Y) = 0 And board(point8.X + 1, point8.Y) = 0 Then
    pacface.Left += 1
    leftd = False
    upd = False
    downd = False
    rightd = True
    End If
    If GetAsyncKeyState(Keys.Left) And board(point1.X - 1, point1.Y) = 0 And board(point2.X - 1, point2.Y) = 0 And board(point3.X - 1, point3.Y) = 0 And board(point4.X - 1, point4.Y) = 0 And board(point5.X - 1, point5.Y) = 0 And board(point6.X - 1, point6.Y) = 0 And board(point7.X - 1, point7.Y) = 0 And board(point8.X - 1, point8.Y) = 0 Then
    pacface.Left -= 1
    upd = False
    downd = False
    rightd = False
    leftd = True
    End If
    If GetAsyncKeyState(Keys.Up) And board(point1.X, point1.Y - 1) = 0 And board(point2.X, point2.Y - 1) = 0 And board(point3.X, point3.Y - 1) = 0 And board(point4.X, point4.Y - 1) = 0 And board(point5.X, point5.Y - 1) = 0 And board(point6.X, point6.Y - 1) = 0 And board(point7.X, point7.Y - 1) = 0 And board(point8.X, point8.Y - 1) = 0 Then
    pacface.Top -= 1
    downd = False
    rightd = False
    leftd = False
    upd = True
    End If
    If GetAsyncKeyState(Keys.Down) And board(point1.X, point1.Y + 1) = 0 And board(point2.X, point2.Y + 1) = 0 And board(point3.X, point3.Y + 1) = 0 And board(point4.X, point4.Y + 1) = 0 And board(point5.X, point5.Y + 1) = 0 And board(point6.X, point6.Y + 1) = 0 And board(point7.X, point7.Y + 1) = 0 And board(point8.X, point8.Y + 1) = 0 Then
    pacface.Top += 1
    upd = False
    leftd = False
    rightd = False
    downd = True
    End If
    End Sub
    Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
    If testmode = True Then
    Dim p As Point
    p = Me.PointToClient(MousePosition)
    Me.Text = String.Format("X: {0}, Y: {1}", p.X, p.Y)
    End If
    End Sub

    Private Sub mouth_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mouth.Tick

    End Sub
    Private Sub drawthedot()
    If Me.InvokeRequired Then
    Dim dot As New drawdot(AddressOf drawthedot)
    Me.Invoke(dot)
    Else
    Dim looppoint As Point
    For loop1 = 0 To 683
    For loop2 = 0 To 779
    looppoint.X = loop1
    looppoint.Y = loop2
    If board(looppoint.X, looppoint.Y) = 0 Then
    Dim shape As New OvalShape
    canvas.Parent = Me
    shape.Parent = canvas
    shape.BackStyle = BackStyle.Opaque
    shape.BackColor = Color.Black
    shape.Size = New System.Drawing.Size(1, 1)
    shape.Location = New System.Drawing.Point(looppoint.X, looppoint.Y)
    shape.Enabled = False

    End If
    Next
    Next
    End If
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles Worker1.DoWork
    Call drawthedot()
    End Sub
    End Class


    another guy was trying to help me so that s why i have all the delagates and calls but it still give me an error

    Please no1 rip off my code
     
  6. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    421
    199
    10
    Generally, you would recieve this error if you try to modify parts of your graphical user interface from another thread than the thread that created it. This problem can be solved by taking advantage of the SynchronizationContext class.
     
  7. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    #7 stevemk14ebr, Jul 10, 2011
    Last edited: Jul 10, 2011
    (OP)
    could u give me a specific code example for my code Ive had people already tell me a bout that but don't know how to incorporate it help any1
     
  8. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    ps never got this working still have no idea