Results 1 to 7 of 7

Thread: [RESOLVED] DoEvents does not resume from its last position - why?

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2021
    Posts
    41

    Resolved [RESOLVED] DoEvents does not resume from its last position - why?

    Update
    My original question:
    Please help me understand why, at some point, DoEvents does not resume from its last position (marked in the code below).
    It will work a few times and then, after DoEvents, the code is not executed and is stuck at bProcessing()=True.
    Cause of the problem:
    Another DoEvents triggered _DataArrival from the same socket (as expected). However, it then got stuck in a loop because there was a missing condition that would allow DoEvents to exit from the procedure and go back to where it was interrupted and resume its work.

    Solution:
    Code:
    If bProcessing(Index) Then Exit Sub
    This is how (in a simplified form) looks my code. It's a server (using standard Winsock) that handles connections from a couple of connected clients (hence the Index to identify them):

    Code:
    Private Sub sckServer_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    
        lBytes(Index) = bytesTotal ' Count received data for each client (Index)
    
        While lBytes(Index) > 0
    
            If bProcessing(Index) Then Exit Sub ' Required condition that allows the DoEvents to exit this procedure and resume its work from the last state
    
            If lBytes(Index) >= 65536 And bProcessing(Index) = False Then ' Let's say I'm expecting 64 kB of data (this is just an example)
                bProcessing(Index) = True
                Call HandleData(Index)
                bProcessing(Index) = False
            End If
    
            DoEvents
    
        Wend
    
    End Sub
    
    Sub HandleData(Index as Integer)
    
        sckServer(Index).GetData byteData, vbArray + vbByte, 65536: lBytes(si) = lBytes(si) - 65536
    	
        For i=1 to nClients
            If i=nPairedClient Then
                sckServer(i).SendData {received data} ' Forward received data to the paired client
                DoEvents
                'This was the place when at some point DoEvents did not resume but thanks to the added condition in sckServer_DataArrival() now it does (read above)
            End If
        Next i
    	
    End Sub
    Last edited by T3mp0; Apr 16th, 2024 at 03:45 AM.

  2. #2
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,120

    Re: DoEvents does not resume from its last position - why?

    I don't think that putting a DoEvents in that async event is a good idea. Because as you have it now while it is processing a DataArrival it can come another DataArrival. And what happens? A mess.

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2021
    Posts
    41

    Re: DoEvents does not resume from its last position - why?

    No, that's why there is bProcessing(). The data should be arriving, I'm expecting it. I assure you it works as intended.

  4. #4
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,747

    Re: [RESOLVED] DoEvents does not resume from its last position - why?

    use peekmessage. it gives u more control of what kind of incoming windows messages u want through.
    usually what we want is to not freeze the mouse/keyboard/form.
    a sleep 0/1 is also good to add so give system some air.

  5. #5
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,120

    Re: DoEvents does not resume from its last position - why?

    Quote Originally Posted by T3mp0 View Post
    No, that's why there is bProcessing(). The data should be arriving, I'm expecting it. I assure you it works as intended.
    So when bProcessing is true and it receives another packet, it looses that packet because it is still processing another prior one...
    If you don't experience that issue is because it does not happen, but something in the logic is not right...

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,199

    Re: [RESOLVED] DoEvents does not resume from its last position - why?

    Is sckServer_DataArrival an event? If so, you're possibly recursing that event with the DoEvents in there. Event-driven programs don't need DoEvents, as the OS (i.e., Windows) will raise the event automatically when it's idling. If it's not idling, then your program is possibly processing the prior sckServer_DataArrival event, and shouldn't be interrupted.

    That's the core philosophy of event-driven software, which basically all Windows programs are.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2021
    Posts
    41

    Re: [RESOLVED] DoEvents does not resume from its last position - why?

    The first post is updated with the solution.

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