|
-
Jun 3rd, 2005, 02:46 PM
#1
Thread Starter
New Member
Doing things simultaneously?
HI, i'm doing a project where I have two pictures and they race eachother at random speeds. My problem is, I do not know how to make them both go at the same time because the first strip of code will always be executed first so it always makes one image go then after that's finished the other one will go. Thanks for any help
-
Jun 3rd, 2005, 02:51 PM
#2
Addicted Member
Re: Doing things simultaneously?
Hi,
Assuming you are using a timer to move the pictures place all "movement" code in the timer event so all of them are moved at same time like:
Code may look like this:
VB Code:
Private Sub Timer1_Timer()
Image1(0).Left = Image1(0).Left + CInt(Rnd * 100) + 1
Image1(1).Left = Image1(1).Left + CInt(Rnd * 100) + 1
End Sub
Have a good one!
BK
Last edited by Black__Knight; Jun 3rd, 2005 at 02:55 PM.
-
Jun 3rd, 2005, 03:12 PM
#3
Thread Starter
New Member
Re: Doing things simultaneously?
ah, thanks. I have no idea why I forgot about timers.
-
Jun 3rd, 2005, 04:22 PM
#4
Re: Doing things simultaneously?
How did the Timer make any difference?
It's still true that Image1(0) will move first! One computer can do only one thing at a time, there will never be a totally simultanous move of two things.
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Jun 3rd, 2005, 04:27 PM
#5
Addicted Member
Re: Doing things simultaneously?
Hi,
Opus:
It isn't the timer but that the controls are being moved at "same" time instead of seeing move one by one.
Of course, one could use threads (which aren't really threads in VB but for a race game..a little overkill.) <g>
Anyways, the post was RESOLVED, Mr. Spock. j/k
Have a good one!
BK
Last edited by Black__Knight; Jun 3rd, 2005 at 04:30 PM.
-
Jun 3rd, 2005, 09:14 PM
#6
Thread Starter
New Member
Re: Doing things simultaneously?
 Originally Posted by Black__Knight
Hi,
Opus:
It isn't the timer but that the controls are being moved at "same" time instead of seeing move one by one.
Of course, one could use threads (which aren't really threads in VB but for a race game..a little overkill.) <g>
Anyways, the post was RESOLVED, Mr. Spock. j/k
Have a good one!
BK
Yeah, and I tested it without random numbers and just used 10 for both, and they came in at the same time so it works.
-
Jun 3rd, 2005, 09:40 PM
#7
Junior Member
Re: Doing things simultaneously?
well, actually pictures don't move...
BluEyes.
-
Jun 4th, 2005, 04:15 AM
#8
Re: Doing things simultaneously?
OK, but than I'm wondering what you have done before. Did you have two different events for each picture to be moved, and those events where triggered by a user input?
Unless you have tons of code between, you wouldn't see the time difference if the moves are within one "strip" of code 8i.e. code that is called by a single event).
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Jun 4th, 2005, 04:23 AM
#9
Re: Doing things simultaneously?
 Originally Posted by opus
OK, but than I'm wondering what you have done before. Did you have two different events for each picture to be moved, and those events where triggered by a user input?
Unless you have tons of code between, you wouldn't see the time difference if the moves are within one "strip" of code 8i.e. code that is called by a single event).
Yes Quite right Opus,
Timer or no timer, that hardly makes a difference.
The above code would work same without timers also.
Pradeep
-
Jun 4th, 2005, 09:20 AM
#10
Re: Doing things simultaneously?
To truly make both go at the same time, you are gonna need to know how to multithread. Multithreading is the process of making code processes run asyncronously. Think of a comb or a fork. And for every leg, a code process is being produced. That should give you an idea. Although I'm not too sure if true multithreading can exist in VB. I've seen some VB programs claim it truly does and others claim it's a multithreading simulation. C++ is where true multithreading can exist.
Another thing. Although timers are easy to work with and help maintain low CPU usage, timers are slow, inaccurate, inconsistant, and gets worse when more timers are running at the same time. For some unknown reason they go 10 times faster than usual on XP machines, making it really inaccurate and inconsistant. Using managed loops will help with speed and accuracy, but that would mean more CPU usage. It's even better when you use time based animation, because it'll actually make any animation and movement work in real time. Cheers
-
Jun 4th, 2005, 09:36 AM
#11
Re: Doing things simultaneously?
 Originally Posted by Jacob Roman
To truly make both go at the same time, you are gonna need to know how to multithread.
But one's still going to execute before the other, even if you have multiple CPUs, because the tasks are still allocated sequentially. You can't give two instructions at the same time, it's impossible.
-
Jun 4th, 2005, 09:41 AM
#12
Re: Doing things simultaneously?
Actually it is possible through multithreading cause otherwise, what would be the point in using it? What he could do instead is use either DirectX or BitBlt to draw two images onto the backbuffer (the surface the user cannot see, and once all the drawing is complete, the backbuffer will flip onto the primary surface (the surface the user can see). That way there, it actually will be going at the same time. Here's the link to the greatest DirectX tutorial on the internet that's for VB:
http://externalweb.exhedra.com/DirectX4VB/index.asp
You can only use DX7 and DX8 on VB though. DX9 was made for VB.NET, C#, and C++.
Last edited by Jacob Roman; Jun 4th, 2005 at 09:45 AM.
-
Jun 4th, 2005, 09:46 AM
#13
Re: Doing things simultaneously?
Yup. In that situation if you still wanted to use multithreading you could probably even draw onto the backbuffer using different threads, and achieve yet another performance boost. But in this case, with a single thread, there is no performance difference. Probably it is even slower, since the drawing has to be done twice (once on to backbuffer and then copied on to screen). It will be effectively simultaneous though.
edit: if my post sounds funny it's because you were editing yours while I was typing
-
Jun 4th, 2005, 09:50 AM
#14
Re: Doing things simultaneously?
Yeah but he wouldn't need multithreading if he were to do the backbuffer drawing technique.
-
Jun 4th, 2005, 09:55 AM
#15
Re: Doing things simultaneously?
 Originally Posted by Jacob Roman
Yeah but he wouldn't need multithreading if he were to do the backbuffer drawing technique.
No, I meant he could take advantage of it if he really wanted to, in order to make the drawing process quicker. I presume he wouldn't want to bother though, I certainly wouldn't.
-
Jun 4th, 2005, 01:03 PM
#16
Thread Starter
New Member
Re: Doing things simultaneously?
 Originally Posted by opus
OK, but than I'm wondering what you have done before. Did you have two different events for each picture to be moved, and those events where triggered by a user input?
Unless you have tons of code between, you wouldn't see the time difference if the moves are within one "strip" of code 8i.e. code that is called by a single event).
This is the code i'm using now:
VB Code:
Private Sub cmdMove_Click()
tmr1.Enabled = True
End Sub
Private Sub Reset()
img1.Left = 480
img2.Left = 480
tmr1.Enabled = False
End Sub
Private Sub tmr1_Timer()
Dim speed As Single, speed2 As Single
Randomize
speed = CInt(Rnd * 20) + 1
speed2 = CInt(Rnd * 20) + 1
Do While img1.Left <= 5180 And img2.Left <= 5180
img1.Left = img1.Left + speed
img2.Left = img2.Left + speed2
Loop
If img1.Left > img2.Left Then
MsgBox "1 wins", , "Race"
ElseIf img2.Left > img1.Left Then
MsgBox "2 wins!", , "Race"
ElseIf img1.Left = img2.Left Then
MsgBox "It Was a tie!", , "Race"
End If
Call Reset
End Sub
Before, I had all the code that's in the timer in the move button. When I had it like that one would go then the other would go after that one was done moving. It seems to work fine this way
-
Jun 4th, 2005, 01:26 PM
#17
Re: Doing things simultaneously?
But moving objects like Imageboxes and Pictureboxes will not work as well as drawing the images onto a backbuffer using BitBlt or DirectX because using objects will move one line at a time on demand, which is not good. How you would want it is by blitting the images onto a backbuffer and by the time all the drawing is complete, you blit it into your form window making it all move at the same time as a result.
-
Jun 4th, 2005, 02:35 PM
#18
Re: Doing things simultaneously?
Hi LeftoverLinguine,
If you used the same code before on the move-button_click, the same should have happened. The timer doesn't help anything, you cal the tiemr with the move-button, the timer does its code and then calls reset, which will stop the timer.
I don't see any difference!!!
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Jun 4th, 2005, 03:02 PM
#19
Re: Doing things simultaneously?
Multithreading doesn't perform more then one thing at the time. It only does one thing for a short while and then switch to another thread and let that execute for a few milliseconds and then keep switching back and forth. By doing so it appears as it's doing more then one thing at the time but it really doesn't.
-
Jun 4th, 2005, 03:24 PM
#20
Thread Starter
New Member
Re: Doing things simultaneously?
 Originally Posted by opus
Hi LeftoverLinguine,
If you used the same code before on the move-button_click, the same should have happened. The timer doesn't help anything, you cal the tiemr with the move-button, the timer does its code and then calls reset, which will stop the timer.
I don't see any difference!!!
I know, I thought the same thing, but it works with the timer and doesn't without it. Try it out yourself if you want.
-
Jun 4th, 2005, 04:10 PM
#21
Re: Doing things simultaneously?
Sorry LeftoverLinguine,
I've tried it with Timer and Move-Button, can't see any difference???????
What difference in motion do you see?
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Jun 4th, 2005, 04:41 PM
#22
Thread Starter
New Member
Re: Doing things simultaneously?
When I do it in the move button the first image will move and after it has finished moving the second one will start moving
-
Jun 5th, 2005, 01:48 AM
#23
Re: Doing things simultaneously?
 Originally Posted by Joacim Andersson
Multithreading doesn't perform more then one thing at the time. It only does one thing for a short while and then switch to another thread and let that execute for a few milliseconds and then keep switching back and forth. By doing so it appears as it's doing more then one thing at the time but it really doesn't.
Hence the reference to multiple CPUs
-
Jun 5th, 2005, 03:04 AM
#24
Re: Doing things simultaneously?
That can't true, using the same code as in your posted timer event. Both images are moved during each loop (image1 is always first for one step, but you won't see the difference snce it would happen at the next system cycle, and you don't have a PC running at 1 hz, don't you?). Please post the code you used on the move button.
And for all the other discussion on multithread and second CPU, I think your problem is something else.
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|