Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous API for Streaming Data from a Hardware Device

I'm writing a library in C#, but I need to make it asynchronous. Normally you expose a set of DLL functions, and they take input parameters, and return a value when finished. But how can I make a library function (callable from C++/Delphi/Etc) that already starts streaming back output while still taking input?

The only solution I see now is to communicate using sockets/pipes/etc, instead of DLL calls.

Does someone have an example how to do this with normal DLL calls?

like image 877
Maestro Avatar asked Feb 01 '12 18:02

Maestro


1 Answers

One good model for a straightforward asynchronous library call (which is located in System.dll) is WebClient.DownloadStringAsync. This method downloads from a Uri asynchronously, and raises the DownloadStringCompleted event whenever it finishes.

Your library could likewise provide a FooAsync method, which doesn't block the current thread but raises a FooDataReceived event whenever some data comes into your library and a FooCompleted event whenever the calculation finishes.

like image 50
Adam Mihalcin Avatar answered Oct 14 '22 19:10

Adam Mihalcin