468,119 Members | 1,972 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,119 developers. It's quick & easy.

Make a synchronous call using asynchronous function

!NoItAll
296 100+
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
Expand|Select|Wrap|Line Numbers
  1. Dim sw as new Stopwatch
  2. sw.start
  3. Do until bsomething = true or sw.elapsedmilliseconds >= TIMEOUT
  4.      Threading.thread.sleep(25)
  5.      Application.DoEvents
  6. Loop
  7. If sw.Elapsedmilliseconds > TIMEOUT then
  8.    'notify user of timeout
  9. Else
  10.    'process the data that arrived inside the asyc function
  11. End if 
  12.  
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...)?
Sep 13 '16 #1
0 785

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by ahmed fat-hi | last post: by
4 posts views Thread by Ivan | last post: by
3 posts views Thread by KaNos | last post: by
11 posts views Thread by Fabio | last post: by
1 post views Thread by bsonline | last post: by
15 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.