Make a synchronous call using asynchronous function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • !NoItAll
    Contributor
    • May 2006
    • 297

    Make a synchronous call using asynchronous function

    There appears to be lots of information about how to asynchronously implement synchronous functions - however I have the opposite problem and there is a dearth of information about it...
    I have a network lib I am using. All calls are asynchronous - that's good. However, I need to make SOME calls synchronously - in that I want the program (or at least the current thread) to stop and wait for the response (or time out). This is for things like filling in a form for a user to read and make selections...
    There are all kinds of ways such as a simple spinwait
    Code:
    Dim sw as new Stopwatch
    sw.start
    Do until bsomething = true or sw.elapsedmilliseconds >= TIMEOUT
         Threading.thread.sleep(25)
         Application.DoEvents
    Loop
    If sw.Elapsedmilliseconds > TIMEOUT then
       'notify user of timeout
    Else
       'process the data that arrived inside the asyc function
    End if
    Maybe this is acceptable, but it seems hacky to me. For one, it requires two global variables - the boolean (bsomething in the example above) and a global to hold the data that arrived asynchronously. Is there a pattern others use for these things?
    Is this a place where a callback makes sense (I've not done any callbacks yet...)?
Working...