¿Existe un patrón para envolver una API síncrona para exponer métodos como Async? [duplicar]

I've been asked to use an API that makes http calls to a server behind the scenes.

Unfortunately, all the calls are synchronous. I'd like to wrap the classes in this API so that I can make async calls, with the ability to cancel calls.

It would be a licensing violation to publish a WCF service that exposes this API.

¿Existe un patrón para hacer esto?

preguntado el 08 de noviembre de 11 a las 16:11

An API being synchronous or asynchronous should be a pure client side decision and not inherent in a service. -

If the API you're calling is synchronous, then there's no way you can do anything to make the asynchronous. You can make the Aparecer to be asynchronous (by wrapping them in either a Task, or just simply using a Thread yourself), but at the end of the day, whichever thread calls that API, will be blocked waiting for the response. -

1 Respuestas

You can always fire off a new Task that runs in a new thread with Task.Factory.StartNew. If you need the competition notification on a non threadpool thread, I recommend using a SynchronizationContext to schedule it to a different thread.

respondido 08 nov., 11:20

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.