Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21096

Alertable Msg Loop, Async I/O

$
0
0
In order to use the async callbacks (completion routines) with many forms of Windows I/O it is necessary for your program to enter an "alertable wait state." Raymond Chen's The alertable wait is the non-GUI analog to pumping messages is one of the few decent writeups on the topic I have read.

But a VB6 program doesn't seem to do so as part of its standard message loop. This means you either go back to a form of polling (Timer event that calls one of the alertable wait functions) or... ?

That "?" is what this question is about. How are others dealing with this?


I have a "working" hack, but it really doesn't feel good to me and I am hoping somebody has a better alternative.

What I'm doing is putting this into a static module:

Code:

Private RunAwaitableMsgLoop As Boolean

Public Sub AwaitableMsgLoop(ByVal RunLoop As Boolean)
    'Call passing True to start awaitable message processing, False to stop.

    If RunLoop And RunAwaitableMsgLoop Then
        'Raise exception, or change this to Exit Sub instead:
        Err.Raise 5, "AwaitableMsgLoop", "AwaitableMsgLoop already running"
    End If
   
    If RunLoop Then
        RunAwaitableMsgLoop = True
        Do
            If MsgWaitForMultipleObjectsEx(0, _
                                          ByVal 0&, _
                                          INFINITE, _
                                          QS_ALLINPUT, _
                                          MWMO_ALERTABLE Or MWMO_INPUTAVAILABLE) <> WAIT_IO_COMPLETION Then
                DoEvents
            End If
        Loop While RunAwaitableMsgLoop
    Else
        RunAwaitableMsgLoop = False
    End If
End Sub

I call this passing True after I start the first async I/O and then again once the work is done passing False.

As I said, it seems to be working fine. However I worry about side effects. In particular I'm concerned about using it when I have a lot of async I/O going on at the same time in the same program.

My real use for this isn't necessarily disk I/O but it ought to be useful for that in many scenarios as well. My simple-minded "testbed" or demo application does this to evaluate the concept though. It just opens and reads a text file (included) in small chunks, displaying them as it goes until EOF or an error.


So once more: Comments? Better alternatives?
Attached Files

Viewing all articles
Browse latest Browse all 21096

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>