0

I believe I've found a similar case with InvalidOperationException - object is currently in use elsewhere; however, that is using c# where as I'm using .net, and I can't understand how to apply it to my program.

My program is an attempt at making a basic replica of the arcade game space invaders, and to control their movement I use background workers. This is where the code usually gets the error:

If Invader4.Left < Me.Width - 75 Then
    Invader4.Left += 3
    If Invader4.Tag = "OrangeSpaceInvader1" Then
        Invader4.Image = My.Resources.OrangeSpaceInvaderBlk2
        Invader4.Tag = "OrangeSpaceInvader2"
    ElseIf Invader4.Tag = "OrangeSpaceInvader2" Then
        Invader4.Image = My.Resources.OrangeSpaceInvaderBlk
        Invader4.Tag = "OrangeSpaceInvader1"
    End If
    Threading.Thread.Sleep(TimeSpan.FromSeconds(X))
End If

This does not only occur for Invader4, as I've seen it pop up regarding Invader13, Invader2, and pretty much all of them. I have 16 in total, Invaders1-16. This specific piece of my code is in regard of moving them across the screen to the right. The line of code that the box containing the error is as the second line,

Invader4.Left += 3

I understand from reading answers on the other questions that multiple threads are trying to access the invader at once, although I'm not sure where in my program that is happening. The program can get this error immediately after I start the background workers controlling the movement of each invader, or when they are half way down, or anywhere in between or after.

Community
  • 1
  • 1
Adam
  • 1
  • `to control their movement I use background workers`. You cannot update UI on a worker thread. – Hans Passant Oct 25 '12 at 00:33
  • What is UI exactly? I've only started programming recently, and I can't say I'm familiar with this term. Also, thanks! That's one step closer none-the-less. – Adam Oct 25 '12 at 00:42
  • User Interface. You cannot update the Image property from a worker. – Hans Passant Oct 25 '12 at 08:13

0 Answers0