Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Multitasking (VB 6.0)

    I need to count the number of keystrokes (perhaps just the space bar) while executing a for/next loop, So that means I need a way to make the key press interrupt the loop long enough to bump a counter. Is there some way to do this in VB?

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Multitasking (VB 6.0)

    I don't understand. Executing a for next loop and keystrokes don't bear any particular relation to one another. A for next loop executes in code, so what keystrokes would you be trapping and why?
    Charlotte

  3. #3
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Multitasking (VB 6.0)

    I realize this is a bit of an odd application, but here is what I'm trying to do:

    I have a for/next loop that briefly displays certain random numbers at a predefined rate. There is a target number that the user is to look for. When the user sees the target number displayed he/she presses the space bar and that bumps a counter. At the end of the exercise I want to compare the actual number of times that the target was displayed vs. the number of times the user pressed the space bar.

    Thus, the keystrokes must work in an interrupt fashion.

    Perhaps what I'm attempting is not possible, but I thought i would give it a try.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Multitasking (VB 6.0)

    Try the following:

    Set the KeyPreview property of the form to True.

    At the top of the form Module, declare two variables:

    Private blnLoop As Boolean
    Private lngCount As Long

    Use code like this to run the loop:

    Private Sub Command1_Click()
    Dim i As Long
    ' Initialize variables
    blnLoop = True
    lngCount = 0
    ' Take focus away from command button
    Me.Text1.SetFocus
    For i = 1 To 100
    ' Your code to display random numbers goes here
    ...
    Next i
    ' Turn off counting
    blnLoop = False
    ' Report number of times space bar was pressed
    MsgBox "The space bar was pressed " & lngCount & " times."
    End Sub

    Command1 and Text1 are just used for illustration purposes, replace them with appropriate names.

    Create an On KeyPress event procedure for the form:

    Private Sub Form_KeyPress(KeyAscii As Integer)
    ' Are we in the loop?
    If blnLoop Then
    ' Did the user press the space bar?
    If KeyAscii = vbKeySpace Then
    ' Yes, increment the counter
    lngCount = lngCount + 1
    End If
    ' Prevent further processing of the keystroke
    KeyAscii = 0
    End If
    End Sub

  5. #5
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Multitasking (VB 6.0)

    Hi Hans,

    Well, we're almost there but not quite. I added the code you suggested, but there is still something missing.

    I inserted the MsgBox right after the Next i, blnLoop = False commands. If I don't press the space bar during the for/next loop, then the Msg Box reports 0 times. If I do press the space bar during the loop, then the program skips the Msg Box entirely and continues on as normal!

    I tried putting a MsgBox in the KeyPress sub, but it never showed up either.

    I must be missing something.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Multitasking (VB 6.0)

    Does your code to display random numbers include a statement Exit Sub? If so, replace it with Exit For and see if it works better.

  7. #7
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Multitasking (VB 6.0)

    Hi Hans,

    There is no Exit inside the For/Next loop so there is nothing to change to Exit For.

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Multitasking (VB 6.0)

    There must be some reason why the code never arrives at the line with the MsgBox instruction...

  9. #9
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Multitasking (VB 6.0)

    I agree, there must be a reason, but it must have something to do with timing. If I run the loop without pressing a key then the MsgBox shows up (with zero key presses, of course). BUT, if I press the space bar while the loop is running, then the MsgBox doesn't show up! It just skips right over it and proceeds. However, I don't find that it has counted the keystrokes when I display lngCount. Here's the OnKey code:

    Private Sub Form_KeyPress(KeyAscii As Integer)
    'are we in the loop?
    If blnLoop Then
    'Did user press space bar?
    If KeyAscii = vbKeySpace Then
    'Yes, increment the counter
    lngCount = lngCount + 1
    End If
    'Prevent further processng of keystroke
    KeyAscii = 0
    End If
    End Sub


    It all looks OK to me.

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Multitasking (VB 6.0)

    Have you set the KeyPreview property of the form to True, as mentioned in <post#=486329>post 486329</post#>?

  11. #11
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Multitasking (VB 6.0)

    Yes, Key Preview for the form is set to True and the form is Enabled. As best as I can determine, the program never enters the KeyPress sub. For example if I put the cursor in the KeyPress sub and then "run to cursor" it never stops at the cursor after I press the key..

    On the one hand, based on the above I would assume that the program never senses the key press. However, something happens because when I don't press the key during the loop I get the MsgBox showing no key presses. When I do press the key the MsgBox gets skipped over! Something very weird is certanly going on.

  12. #12
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Multitasking (VB 6.0)

    I have a feeling, although I can't prove it, that you can't do this in this manner because VB and its resulting programs are single threaded. Essentially you are interrupting the processing in the For...Next loop by pressing a key but the KeyPress event will not resume processing in the For...Next loop as it really doesn't know how to.

    You can do this quite easily in VB .NET with the System.Threading namespace but that doesn't help here.

    Attached is a very quick, very dirty VB project that uses a Timer to create a fake multi-threaded application that displays random numbers in a label and adds 1 to a counter if you press a key when the value is even.

    If you have plenty of time and are a masochist you could use the threading API functions and create a truly multi-threaded application. If you do please post back a new picture of yourself after the inevitable hair loss from pulling it out. One of the reasons I have abandoned VB for VB .NET is the ability of VB .NET to easily create multi-threaded applications. (I use multi-threading in communications applications across our network).

    As always I hope this helps.

    Regards,
    Kevin Bell

  13. #13
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Multitasking (VB 6.0)

    Thanks Kevin -- I was afraid that I was attempting something that VB doesn't handle. As it turns out I own a copy of VB.NET but I really haven't gotten into it yet. The application I'm working on is basically a modification of one that I developed in VB, so the easiest thing was to stick with VB. It seems to me that trying to "fool" VB into some sort of interrupt routine similar to what you suggested would perhaps be more difficult than switching to VB.NET. Besides, it would save me a lot of hair -- or at least preserve the little bit I have left!

    I once tried to import the VB app into VB.NET, but the conversion routine left me with several very confusing errors that I was never able to figure out. But it might work better if I just started from scratch in VB.NET. I'm somewhat of a novice with VB, and tackling VB.NET seems rather daunting, but I suppose I could give it a try. I have no clue as to what "system threading namespace' involves -- sounds rather complicated. Do you recommend any particular book or manual on VB.NET?

  14. #14
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Multitasking (VB 6.0)

    Make sure that you call DoEvents in your For ... Next loop.

    The attached crude mini-project works for me.

  15. #15
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Multitasking (VB 6.0)

    I stand corrected and bow to HansV's superior programming skills.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •