My Turn - Threading

  • Thread starter One Handed Man \( OHM - Terry Burns \)
  • Start date
O

One Handed Man \( OHM - Terry Burns \)

I havent played much with threading and Im testing stuff out with a little
space invaders game. My Invader class has a StopInvader StartInvader and
Invade subs.

There is a class level variable called Alive of Type Boolean.

On StartInvader I begin a new thread at the address of Invade and inside
this While Alive . . . . . . . Do Stuff ( Move etc ) Wend.

Stop invalder simply sets Alive to False.

OK

So the start starts it OK, and it shoots across the screeen, Fine; Stop
Stops the Invade, however, StartInvader only works the first time,

any ideas ? ( Excuse any bad programming as Im only playing )


Public Class Invader

Private invaderImage As Image

Private invaderImageDelete As Image

Private space As Graphics

Private startPoint As Point

Private currentPoint As New Point

Private Enum direction

left

right

End Enum

Private invaderDirection As direction

Private alive As Boolean = True

Private parentForm As Form1

Private invadeThread As Threading.Thread



Public Sub New(ByVal iSpace As Graphics, ByVal refForm As Form1, ByVal
iInvader As String, ByVal iInvaderDelete As String)

space = iSpace

invaderDirection = direction.right

startPoint = New Point(10, 10)

parentForm = refForm

invaderImage = Image.FromFile(iInvader)

invaderImageDelete = Image.FromFile(iInvaderDelete)

End Sub

Public Sub startInvader()

currentPoint.X = startPoint.X

currentPoint.Y = startPoint.Y

parentForm.newGameButton.Enabled = False

parentForm.stopGameButton.Enabled = True

invadeThread = New Threading.Thread(AddressOf invade)

invadeThread.IsBackground = True

invadeThread.Start()

End Sub



Public Sub stopInvader()

parentForm.newGameButton.Enabled = True

parentForm.stopGameButton.Enabled = False

alive = False

End Sub

Private Sub Fire()





End Sub

Private Sub invade()

While alive

If currentPoint.X > space.VisibleClipBounds.Width Then

currentPoint.X = startPoint.X

currentPoint.Y = startPoint.Y

End If

Threading.Thread.CurrentThread.Sleep(20)

space.DrawImage(MyClass.invaderImageDelete, currentPoint)

currentPoint.X += 5

space.DrawImage(MyClass.invaderImage, currentPoint)

End While

End Sub



--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--
 
I

Imran Koradia

Just a guess - You're setting alive to false when calling StopInvader which
is never being set to true again which is why the second time Invade just
skips the entire while loop. Or did I overlook something :)??


Imran.
 
O

One Handed Man \( OHM - Terry Burns \)

Bang On - I just didnt see it ( What an idiot I am )

Thanks

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
--


Imran Koradia said:
Just a guess - You're setting alive to false when calling StopInvader
which is never being set to true again which is why the second time Invade
just skips the entire while loop. Or did I overlook something :)??


Imran.
 
O

One Handed Man \( OHM - Terry Burns \)

Imran was correct, but thanks for answering

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
O

One Handed Man \( OHM - Terry Burns \)

LOL, for what its worth, yes, i would be happy to post it.



--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
O

One Handed Man \( OHM - Terry Burns \)

No thats fine really, Im happy for any input to my posts.

Cheers - OHM

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top