Results 1 to 22 of 22

Thread: Looping problem...

  1. #1

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205

    Looping problem...

    I am trying to make a form flash colours whilst some other activity takes place, and then stop. I use the following functions to flash the form.

    VB Code:
    1. Private Function RunWalkthrough_0_1()
    2.     'Explain the display
    3.     Call FlashForm(frmControlHead)
    4.    'Do stuff.... lalala
    5.     Call StopFlashControl
    6. End Function
    7.  
    8. Public Sub FlashForm(frmForm As Form, Optional ColourFlash = -1)
    9. On Error Resume Next
    10.     'Flash form between two colours every second
    11.     flgFlashControl = True
    12.    
    13.     'Set flash colour and original colour
    14.     Dim OriginalColour As Long, FlashColour As Long
    15.     FlashColour = IIf((ColourFlash < 0), DEFAULT_FLASH_COLOUR, ColourFlash)
    16.     OriginalColour = frmForm.BackColor
    17.    
    18.     'Flash form
    19.     Do While (flgFlashControl)
    20.         DoEvents
    21.         'Swap colours
    22.         If frmForm.BackColor = FlashColour Then
    23.             frmForm.BackColor = OriginalColour
    24.         Else
    25.             frmForm.BackColor = FlashColour
    26.         End If
    27.         'Wait
    28.         PauseMe (50000)
    29.     Loop
    30.    
    31.     'Return form colour
    32.     frmForm.BackColor = OriginalColour
    33. End Sub
    34.  
    35. Public Sub StopFlashControl()
    36.     'Reset flashing flag
    37.     flgFlashControl = False
    38. End Sub
    39.  
    40.  
    41. 'Pause system in a loop temporarily
    42. Public Sub PauseMe(lngPauseDuration As Long)
    43.     Dim i As Long
    44.     Do Until i = lngPauseDuration
    45.         i = i + 1
    46.         DoEvents
    47.     Loop
    48. End Sub

    But the code never seems to leave the FlashForm function. StopFlashControl is never callled. Bit of help, please?
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  2. #2
    Junior Member
    Join Date
    Feb 2002
    Posts
    24
    i could be wrong......but it looks like you never get out of your do while loop in FlashForm....

    it looks like flgFlashControl is always true

    so it never gets to the end of FlashForm.....hence never getting back to RunWalkthrough_0_1 hence never getting to StopFlashControl

    i am new to vb so please ignore post if i am off base! - lol

  3. #3
    Fanatic Member
    Join Date
    Jun 2001
    Posts
    521
    You'll have to use a timer control, and place some of the code in there. With it coded as currently, executing won't continue until the function Flash... returns. What you need is a timer event that sets the flash flag to false at some point... or even better, place the flashing code in a timer with an interval of 2000, then enable the timer where you call the Flash... function, and disable the timer when you want flashing to cease.

  4. #4
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    flgFlashControl is never changed within your Do Loop, so it will always be True so the loop will just keep going and going...

  5. #5

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    I'm aware that the flag won't change within the loop, but it's a public flag, and I thought by calling another function, it would set it false.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  6. #6
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    It will. Try
    VB Code:
    1. Public flgFlashControl
    2. Private Sub Command1_Click()
    3.     RunWalkthrough_0_1
    4. End Sub
    5.  
    6. Private Sub Command2_Click()
    7.     Call StopFlashControl
    8. End Sub

  7. #7

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    But it's not. That's the problem...
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  8. #8
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    I see. DoEvents lets you "jump" out of the VB code and fire a new event. But I think you want to just have the code run two functions at the same time. Timer control solution would work. Otherwise maybe someone else can provide some type of threading/hooking sort of explanation or solution.

  9. #9

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Threading would probably solve it, but I gather VB isn't so good at that.

    Have got a ****ty workaround for now. Time constraints and all that.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  10. #10
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    rjlohan - Gday,

    You could place a counter in the loop, and when that value is reaced
    Exit Do, Or

    Set an external Boolean Flag, that if detected in your loop will
    Exit Do.

    (Either way you go do without the StopFlashing Sub and handle
    the 'Stop' within the Loop 'Counter, or Bololean Flag)

    As mentioned, the problem is you will never get to the StopFlashing Sub, cause ur in a loop (as you know).



    Bruce.

  11. #11

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Counter's no good unfortunately, as the end of the loop varies based on the length (in seconds) of an audio file. I'm trying to do some automated walkthrough stuff in a hurry, so I'm just gonna go with my 'hack' solution for now.

    Cheers,
    RJ
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  12. #12
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    What are you trying to do rj?

    Stop the flashing of the form?

  13. #13
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    I think he wants the form to flash while VB is running other code. I'd use the Timer control. Thought that's what Timer was for. But I don't know.

  14. #14

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    See, the problem is, that I'm also playing audio, and I want to synchronise a series of events. It's a right pain in the arse, I tell you...
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  15. #15
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    Ok how's this:

    You'll need a command button a timer! Just for testing! Set the timer enabled to false to start with and it's interval to 5000!
    VB Code:
    1. Dim flgFlashControl As Boolean
    2.  
    3. Private Function RunWalkthrough_0_1()
    4.     'Explain the display
    5.     Call FlashForm(frmControlHead)
    6.    'Do stuff.... lalala
    7.     Call StopFlashControl
    8. End Function
    9.  
    10. Public Sub FlashForm(frmForm As Form, Optional ColourFlash = -1)
    11. On Error Resume Next
    12.     'Flash form between two colours every second
    13.     Call writing
    14.    
    15.     'Set flash colour and original colour
    16.     Dim OriginalColour As Long, FlashColour As Long
    17.     FlashColour = IIf((ColourFlash < 0), DEFAULT_FLASH_COLOUR, ColourFlash)
    18.     OriginalColour = frmForm.BackColor
    19.    
    20.     'Flash form
    21.     DoEvents
    22.     Do While (flgFlashControl = True)
    23.         DoEvents
    24.         'Swap colours
    25.         If frmForm.BackColor = FlashColour Then
    26.             frmForm.BackColor = OriginalColour
    27.         Else
    28.             frmForm.BackColor = FlashColour
    29.         End If
    30.         'Wait
    31.         PauseMe (10000)
    32.     Loop
    33.     DoEvents
    34.     'Return form colour
    35.     frmForm.BackColor = OriginalColour
    36. End Sub
    37.  
    38. Public Sub StopFlashControl()
    39.     'Reset flashing flag
    40.     flgFlashControl = False
    41. End Sub
    42.  
    43. 'Pause system in a loop temporarily
    44. Public Sub PauseMe(lngPauseDuration As Long)
    45. DoEvents
    46.     Dim i As Long
    47.     Do Until i = lngPauseDuration
    48.         DoEvents
    49.         i = i + 1
    50.         DoEvents
    51.     DoEvents
    52.     Loop
    53.     DoEvents
    54. End Sub
    55.  
    56. Private Sub Command1_Click()
    57. flgFlashControl = True
    58. Call FlashForm(Me, red)
    59. End Sub
    60.  
    61.  
    62. Public Sub writing()
    63. Timer1.Enabled = True
    64. End Sub
    65.  
    66. Private Sub Timer1_Timer()
    67. Text1.Text = "jbjabsdlvkbas"
    68. flgFlashControl = False
    69. End Sub

    The Writing sub doesnt have to be launching a timer it could play music as well!

    b

  16. #16
    Fanatic Member rudvs2's Avatar
    Join Date
    Mar 2001
    Location
    NZ
    Posts
    935
    Best solution

    Ditch vb6 get vb.net and use multithreading to solve your problem

    Much less code too



    Glad to be of service to ya

  17. #17
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    RJ,

    Is there an event for the Audio file 'Finished'/'Load Complete' (or whatever it is) ?

  18. #18

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Only if I use that MCI control, which I'm not.
    I'm using sndPlaySound API. I'm sure I had a reason at some stage.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  19. #19
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    i assume my way didnt work for you?

  20. #20

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Wasn't really appropriate to the situation I'm afraid.

    I'm thinking I may try the MCI control, and use its events to help me. But that will only work if I can implement function pointers in VB. Is that possible?
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  21. #21
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    Originally posted by rjlohan
    Wasn't really appropriate to the situation I'm afraid.

    I'm thinking I may try the MCI control, and use its events to help me. But that will only work if I can implement function pointers in VB. Is that possible?
    What there's a situation where a carton of beer doesnt help??

    *covers ears*

  22. #22

    Thread Starter
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    When did you say anything about beer??
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

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