c# - How to call an Async API in a synchronous fashion? -


suppose have typical async design-pattern api has

  1. a method called start launches async process
  2. a callback called when process completes

how call api in synchronous fashion using .net 3.5 without blocking message pump during call?

if want wait result (synchronous call) not want block message pump (assuming mean ui thread) have use separate thread call (and wait it)

using async , await:

// current thread ui thread // create/use task wait call var task = task.factory.startnew( () => { dojobasync().wait()}); 

the ui thread creates task (t1) , returns immediately. t1 runs on different thread. thread calls asynchronous function (it returns task t2) wait result of asynchronous t1 calls wait on t2.

this pretty contrived:

  1. the intend of asynchronous method executed asynchronously, because take quite time finish.
  2. the task t1 allows ui continue next lines of calling code (opposed using async/await ui thread registers continuation. might need resulks t1 in other way.

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -