liblsl-Csharp
liblsl-Csharp copied to clipboard
Async wrapper
To me it also makes sense to wrap pull_sample (and even more especially resolve_streams) into async methods because it is a very convenient way to kick off a method that handles samples as they come in in the background. When a C# method hits await, it returns control to the caller. Then, anything that happens after await will kick off automagically once the task is complete:
private async Task PullAndProcessAsync()
{
await Task.Run(()=>pull_sample(...));
// at this point the caller is back on
ProcessNewData(...);
// processing of data is happening outside of, say, GUI thread
}
Waiting on IO is exactly what async methods that return Task are meant for. CPU intensive tasks are meant to return Task<T>. This also goes hand in hand with leaning on C# to handle allocating buffers and collecting garbage instead of the C-style array arguments for the output from pull_sample/chunk.
I am not sure if the best thing to do is to leave this library as it is and simply add a class that wraps everything in async methods, or just to roll a new wrapper. People are already using this one, but it is rather 2009.
Originally posted by @dmedine in https://github.com/labstreaminglayer/liblsl-Csharp/issues/12#issuecomment-833987771