1

Recently in C# 4.0 the task based approach for async programming was unveiled. So we were trying to develop some of our functions which used callbacks earlier.

The problem we are facing is with the implementation of multiple responses for the functions using tasks. E.g. we have a function which fetches some data from a thirdparty API. But before fetching the data from API we first check whether we already have it in our in-memory cache or in DB then only we go to the API. The main client application sends a request for a list of symbols for which data to fetch. If we find data for some symbols in cache or in DB we send it immediately via the callback. For remaining symbols we request the API.

This gives a feeling of real-time processing on client application for some symbols. And for other symbols the user gets to know that it will take time. If I do not send responses to the client instantly and first collect all the data and then only send response for the whole list then the user will be stuck for 99 symbols even if only 1 symbol is to be fetched from API.

How can I send multiple responses using the task based approach?

puneet
  • 769
  • 1
  • 9
  • 34
  • possible duplicate of [C# async/await Progress event on Task<> object](http://stackoverflow.com/questions/15408148/c-sharp-async-await-progress-event-on-task-object) – Matías Fidemraizer Apr 29 '14 at 06:33
  • It's not a duplicate. The reporting of progress and returning actual data in blocks are a different problems. I don't understand why people these days have started over-moderating things. If you do not add to the answer or the question at-least let others with a different view have a look. – puneet Apr 29 '14 at 06:43
  • 1
    FYI: http://stackoverflow.com/q/22829844/1768303. On a side note, `async/await` was added in C# 5.0, not 4.0. If that's what you meant, you may want to edit the question's tags. – noseratio Apr 29 '14 at 08:01
  • @Noseratio thanks for the link but it does nto solve my problem. I have a similar function but argument accepts a List and current callbacks return data for each string individually – puneet Apr 29 '14 at 08:17
  • 1
    @puneet, another approach is to use `Task.FromResult` for your cached items. You should add some code to your question if you want a more concrete answer. – noseratio Apr 29 '14 at 08:24
  • Or store in cache a completed task. – Paulo Morgado Apr 29 '14 at 12:01
  • @puneet It's not over-moderating. I marked as a possible duplicate because I feel answer to the suggested question may give you a hint, and one of these hints is that tasks aren't designed for what you're trying to achieve. – Matías Fidemraizer Apr 29 '14 at 18:49

1 Answers1

0

It seems like you want to have an async method that returns more than once. The answer is you can't. What you can do is:

  1. Call 2 different methods with the same "symbols". The first only checks the cache and DB and returns what it can, and the second one only calls the remote API. This way you get what you can fast from a cache and the rest more slowly.
  2. Keep using callbacks as a "mini producer consumer" design so you can call it as many times you like.

I could try for a more concrete answer if you post the code you're using.

i3arnon
  • 113,022
  • 33
  • 324
  • 344