473,802 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

My Turn - Threading

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 invaderImageDel ete As Image

Private space As Graphics

Private startPoint As Point

Private currentPoint As New Point

Private Enum direction

left

right

End Enum

Private invaderDirectio n As direction

Private alive As Boolean = True

Private parentForm As Form1

Private invadeThread As Threading.Threa d

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

space = iSpace

invaderDirectio n = direction.right

startPoint = New Point(10, 10)

parentForm = refForm

invaderImage = Image.FromFile( iInvader)

invaderImageDel ete = Image.FromFile( iInvaderDelete)

End Sub

Public Sub startInvader()

currentPoint.X = startPoint.X

currentPoint.Y = startPoint.Y

parentForm.newG ameButton.Enabl ed = False

parentForm.stop GameButton.Enab led = True

invadeThread = New Threading.Threa d(AddressOf invade)

invadeThread.Is Background = True

invadeThread.St art()

End Sub

Public Sub stopInvader()

parentForm.newG ameButton.Enabl ed = True

parentForm.stop GameButton.Enab led = False

alive = False

End Sub

Private Sub Fire()

End Sub

Private Sub invade()

While alive

If currentPoint.X > space.VisibleCl ipBounds.Width Then

currentPoint.X = startPoint.X

currentPoint.Y = startPoint.Y

End If

Threading.Threa d.CurrentThread .Sleep(20)

space.DrawImage (MyClass.invade rImageDelete, currentPoint)

currentPoint.X += 5

space.DrawImage (MyClass.invade rImage, currentPoint)

End While

End Sub

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

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmv ujpotXjui/OFU".ToCharArra y()
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))
--

Nov 21 '05 #1
10 1095
Instead of "stop" try "sleep".

"One Handed Man ( OHM - Terry Burns )" <news.microsoft .com> wrote in
message news:#2******** ******@TK2MSFTN GP12.phx.gbl:
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 invaderImageDel ete As Image

Private space As Graphics

Private startPoint As Point

Private currentPoint As New Point

Private Enum direction

left

right

End Enum

Private invaderDirectio n As direction

Private alive As Boolean = True

Private parentForm As Form1

Private invadeThread As Threading.Threa d

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

space = iSpace

invaderDirectio n = direction.right

startPoint = New Point(10, 10)

parentForm = refForm

invaderImage = Image.FromFile( iInvader)

invaderImageDel ete = Image.FromFile( iInvaderDelete)

End Sub

Public Sub startInvader()

currentPoint.X = startPoint.X

currentPoint.Y = startPoint.Y

parentForm.newG ameButton.Enabl ed = False

parentForm.stop GameButton.Enab led = True

invadeThread = New Threading.Threa d(AddressOf invade)

invadeThread.Is Background = True

invadeThread.St art()

End Sub

Public Sub stopInvader()

parentForm.newG ameButton.Enabl ed = True

parentForm.stop GameButton.Enab led = False

alive = False

End Sub

Private Sub Fire()

End Sub

Private Sub invade()

While alive

If currentPoint.X > space.VisibleCl ipBounds.Width Then

currentPoint.X = startPoint.X

currentPoint.Y = startPoint.Y

End If

Threading.Threa d.CurrentThread .Sleep(20)

space.DrawImage (MyClass.invade rImageDelete, currentPoint)

currentPoint.X += 5

space.DrawImage (MyClass.invade rImage, currentPoint)

End While

End Sub


Nov 21 '05 #2
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.

"One Handed Man ( OHM - Terry Burns )" <news.microsoft .com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
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 invaderImageDel ete As Image

Private space As Graphics

Private startPoint As Point

Private currentPoint As New Point

Private Enum direction

left

right

End Enum

Private invaderDirectio n As direction

Private alive As Boolean = True

Private parentForm As Form1

Private invadeThread As Threading.Threa d

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

space = iSpace

invaderDirectio n = direction.right

startPoint = New Point(10, 10)

parentForm = refForm

invaderImage = Image.FromFile( iInvader)

invaderImageDel ete = Image.FromFile( iInvaderDelete)

End Sub

Public Sub startInvader()

currentPoint.X = startPoint.X

currentPoint.Y = startPoint.Y

parentForm.newG ameButton.Enabl ed = False

parentForm.stop GameButton.Enab led = True

invadeThread = New Threading.Threa d(AddressOf invade)

invadeThread.Is Background = True

invadeThread.St art()

End Sub

Public Sub stopInvader()

parentForm.newG ameButton.Enabl ed = True

parentForm.stop GameButton.Enab led = False

alive = False

End Sub

Private Sub Fire()

End Sub

Private Sub invade()

While alive

If currentPoint.X > space.VisibleCl ipBounds.Width Then

currentPoint.X = startPoint.X

currentPoint.Y = startPoint.Y

End If

Threading.Threa d.CurrentThread .Sleep(20)

space.DrawImage (MyClass.invade rImageDelete, currentPoint)

currentPoint.X += 5

space.DrawImage (MyClass.invade rImage, currentPoint)

End While

End Sub

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

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmv ujpotXjui/OFU".ToCharArra y()
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))
--

Nov 21 '05 #3
Imran was correct, but thanks for answering

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

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmv ujpotXjui/OFU".ToCharArra y()
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))
--
"scorpion53 061" <ad***@nospamhe rekjmsolutions. com> wrote in message
news:OC******** ******@TK2MSFTN GP09.phx.gbl...
Instead of "stop" try "sleep".

"One Handed Man ( OHM - Terry Burns )" <news.microsoft .com> wrote in
message news:#2******** ******@TK2MSFTN GP12.phx.gbl:
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 invaderImageDel ete As Image

Private space As Graphics

Private startPoint As Point

Private currentPoint As New Point

Private Enum direction

left

right

End Enum

Private invaderDirectio n As direction

Private alive As Boolean = True

Private parentForm As Form1

Private invadeThread As Threading.Threa d

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

space = iSpace

invaderDirectio n = direction.right

startPoint = New Point(10, 10)

parentForm = refForm

invaderImage = Image.FromFile( iInvader)

invaderImageDel ete = Image.FromFile( iInvaderDelete)

End Sub

Public Sub startInvader()

currentPoint.X = startPoint.X

currentPoint.Y = startPoint.Y

parentForm.newG ameButton.Enabl ed = False

parentForm.stop GameButton.Enab led = True

invadeThread = New Threading.Threa d(AddressOf invade)

invadeThread.Is Background = True

invadeThread.St art()

End Sub

Public Sub stopInvader()

parentForm.newG ameButton.Enabl ed = True

parentForm.stop GameButton.Enab led = False

alive = False

End Sub

Private Sub Fire()

End Sub

Private Sub invade()

While alive

If currentPoint.X > space.VisibleCl ipBounds.Width Then

currentPoint.X = startPoint.X

currentPoint.Y = startPoint.Y

End If

Threading.Threa d.CurrentThread .Sleep(20)

space.DrawImage (MyClass.invade rImageDelete, currentPoint)

currentPoint.X += 5

space.DrawImage (MyClass.invade rImage, currentPoint)

End While

End Sub

Nov 21 '05 #4
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/cvsotAhsfbuTpmv ujpotXjui/OFU".ToCharArra y()
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" <no****@microso ft.com> wrote in message
news:ub******** ******@TK2MSFTN GP10.phx.gbl...
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.

"One Handed Man ( OHM - Terry Burns )" <news.microsoft .com> wrote in
message news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
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 invaderImageDel ete As Image

Private space As Graphics

Private startPoint As Point

Private currentPoint As New Point

Private Enum direction

left

right

End Enum

Private invaderDirectio n As direction

Private alive As Boolean = True

Private parentForm As Form1

Private invadeThread As Threading.Threa d

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

space = iSpace

invaderDirectio n = direction.right

startPoint = New Point(10, 10)

parentForm = refForm

invaderImage = Image.FromFile( iInvader)

invaderImageDel ete = Image.FromFile( iInvaderDelete)

End Sub

Public Sub startInvader()

currentPoint.X = startPoint.X

currentPoint.Y = startPoint.Y

parentForm.newG ameButton.Enabl ed = False

parentForm.stop GameButton.Enab led = True

invadeThread = New Threading.Threa d(AddressOf invade)

invadeThread.Is Background = True

invadeThread.St art()

End Sub

Public Sub stopInvader()

parentForm.newG ameButton.Enabl ed = True

parentForm.stop GameButton.Enab led = False

alive = False

End Sub

Private Sub Fire()

End Sub

Private Sub invade()

While alive

If currentPoint.X > space.VisibleCl ipBounds.Width Then

currentPoint.X = startPoint.X

currentPoint.Y = startPoint.Y

End If

Threading.Threa d.CurrentThread .Sleep(20)

space.DrawImage (MyClass.invade rImageDelete, currentPoint)

currentPoint.X += 5

space.DrawImage (MyClass.invade rImage, currentPoint)

End While

End Sub

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

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmv ujpotXjui/OFU".ToCharArra y()
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))
--


Nov 21 '05 #5
Do we get it when it is ready?

Cor
Nov 21 '05 #6
good question Cor :)

"Cor Ligthert" <no************ @planet.nl> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
Do we get it when it is ready?

Cor

Nov 21 '05 #7
"One Handed Man ( OHM - Terry Burns )" <news.microsoft .com> wrote in message
news:uQ******** ********@TK2MSF TNGP10.phx.gbl. ..
Bang On - I just didnt see it ( What an idiot I am )


naah - maybe you just a little break :)
Nov 21 '05 #8
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/cvsotAhsfbuTpmv ujpotXjui/OFU".ToCharArra y()
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" <no****@microso ft.com> wrote in message
news:Oj******** *****@TK2MSFTNG P09.phx.gbl...
good question Cor :)

"Cor Ligthert" <no************ @planet.nl> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
Do we get it when it is ready?

Cor


Nov 21 '05 #9
I was just guessing. I didn't have time to pull your code part.

"One Handed Man ( OHM - Terry Burns )" <news.microsoft .com> wrote in
message news:uS******** ******@TK2MSFTN GP12.phx.gbl:
Imran was correct, but thanks for answering


Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

65
6767
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the language's 30+ year evolution. What to you think python largest compromises are? The three that come to my mind are significant whitespace, dynamic typing, and that it is interpreted - not compiled. These three put python under fire and cause...
2
2994
by: Egor Bolonev | last post by:
hi all my program terminates with error i dont know why it tells 'TypeError: run() takes exactly 1 argument (10 given)' =program==================== import os, os.path, threading, sys def get_all_files(path): """return all files of folder path, scan with subfolders
16
2903
by: aurora | last post by:
Hello! Just gone though an article via Slashdot titled "The Free Lunch Is Over: A Fundamental Turn Toward Concurrency in Software" http://www.gotw.ca/publications/concurrency-ddj.htm]. It argues that the continous CPU performance gain we've seen is finally over. And that future gain would primary be in the area of software concurrency taking advantage hyperthreading and multicore architectures. Perhaps something the Python interpreter...
43
16092
by: dan baker | last post by:
I have a page that gets loaded with a meta-refresh hardcoded so that a few things on the page get updated. its kind of a fake chat board. anyway, what I need to do is turn off the meta-refresh once someone clicks in a <textarea> to enter their input; otherwise the refresh catches them in the middle and messes up the focus. I need a way to turn off the meta-refresh, or to force the cursor/focus to stay in the message input box once they...
77
5391
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for the moment. I'd be *very* grateful if people with any interest in multi-threading would read it (even just bits of it - it's somewhat long to go through the whole thing!) to check for accuracy, effectiveness of examples, etc. Feel free to mail...
6
555
by: CK | last post by:
I have the following code in a windows service, when I start the windows service process1 and process2 work fine , but final process (3) doesnt get called. i stop and restart the windows service and the final process(3) gets called. what am I doing wrong with the threading? by the way Directory.GetFiles(IncomingXMLPath1).Length is some global outcome from process 1. Thanks 1)
2
2249
by: Vjay77 | last post by:
In this code: Private Sub downloadBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) If Not (Me.downloadUrlTextBox.Text = "") Then Me.outputGroupBox.Enabled = True Me.bytesDownloadedTextBox.Text = "" Me.totalBytesTextBox.Text = ""
3
2184
by: Jamie Risk | last post by:
I'm attempting to improve some serially executing code (that uses the SerialPort class) bogging Windows down when it runs. To do the 'antibogging' I'm following the example from MSDN Windows.IO.Ports.SerialPort page and use threading. I'm not sure if I'm creating problems with this implementation and would appreciate your input. The original serial code: {
7
2377
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has finished. I am trying to follow this example : http://www.codeproject.com/cs/miscctrl/progressdialog.asp But although the messages still get moved, the progress window never does anything. Here is my code in full, if anybody who knows...
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10538
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.