Results 1 to 24 of 24

Thread: Doing things simultaneously?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    12

    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

  2. #2
    Addicted Member
    Join Date
    May 2005
    Posts
    168

    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:
    1. Private Sub Timer1_Timer()
    2.     Image1(0).Left = Image1(0).Left + CInt(Rnd * 100) + 1
    3.     Image1(1).Left = Image1(1).Left + CInt(Rnd * 100) + 1
    4. End Sub

    Have a good one!
    BK
    Last edited by Black__Knight; Jun 3rd, 2005 at 02:55 PM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    12

    Re: Doing things simultaneously?

    ah, thanks. I have no idea why I forgot about timers.

  4. #4
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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!

  5. #5
    Addicted Member
    Join Date
    May 2005
    Posts
    168

    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.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    12

    Re: Doing things simultaneously?

    Quote 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.

  7. #7
    Junior Member BluEyes's Avatar
    Join Date
    May 2005
    Location
    B.A. - Argentina
    Posts
    21

    Re: Doing things simultaneously?

    well, actually pictures don't move...
    BluEyes.

  8. #8
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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!

  9. #9
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Doing things simultaneously?

    Quote 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
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  10. #10
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    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

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Doing things simultaneously?

    Quote 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.

  12. #12
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    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++.

  13. #13
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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

  14. #14
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Doing things simultaneously?

    Yeah but he wouldn't need multithreading if he were to do the backbuffer drawing technique.

  15. #15
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Doing things simultaneously?

    Quote 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.

  16. #16

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    12

    Re: Doing things simultaneously?

    Quote 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:
    1. Private Sub cmdMove_Click()
    2.     tmr1.Enabled = True
    3. End Sub
    4.  
    5. Private Sub Reset()
    6.     img1.Left = 480
    7.     img2.Left = 480
    8.     tmr1.Enabled = False
    9. End Sub
    10.  
    11.  
    12. Private Sub tmr1_Timer()
    13.     Dim speed As Single, speed2 As Single
    14.     Randomize
    15.     speed = CInt(Rnd * 20) + 1
    16.     speed2 = CInt(Rnd * 20) + 1
    17.     Do While img1.Left <= 5180 And img2.Left <= 5180
    18.         img1.Left = img1.Left + speed
    19.         img2.Left = img2.Left + speed2
    20.     Loop
    21.     If img1.Left > img2.Left Then
    22.         MsgBox "1 wins", , "Race"
    23.     ElseIf img2.Left > img1.Left Then
    24.         MsgBox "2 wins!", , "Race"
    25.     ElseIf img1.Left = img2.Left Then
    26.         MsgBox "It Was a tie!", , "Race"
    27.     End If
    28.     Call Reset
    29. 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

  17. #17
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    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.

  18. #18
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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!

  19. #19
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    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.

  20. #20

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    12

    Re: Doing things simultaneously?

    Quote 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.

  21. #21
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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!

  22. #22

    Thread Starter
    New Member
    Join Date
    Jun 2005
    Posts
    12

    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

  23. #23
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Doing things simultaneously?

    Quote 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

  24. #24
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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
  •  



Click Here to Expand Forum to Full Width