By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,156 Members | 1,069 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,156 IT Pros & Developers. It's quick & easy.

Make a synchronous call using asynchronous function

!NoItAll
100+
P: 296
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
Share this question for a faster answer!
Share on Google+

Post your reply

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