Reputation: 68770
I have built a helper class that talks to a JSON service. The class does its work in a background thread. When done, it calls an Action<> CallBack which the client sent it.
Is it best to have the helper class call the Action<> callback on the main UI thread, or should threading be the responsibility of the client?
Upvotes: 3
Views: 203
Reputation: 62514
I would preffer to have an asynchronous service which able to receive and send data simultaneously, this increases reliability of service as well. This approach allows to avoid Slow Consumer problem which often happens in case of client-side problems, so a service would be safe from such kind of slow/problematic clients.
Regarding UI thread, this is a responsibility of a client. Basically client is responsible to dispatch action on UI thread.
EDIT: Skomski's answer recalled me an approach we've used recently
Basically service provides methods like following:
void Subscribe(IEnumerable<TMessage> messages);
void Subscribe(IEnumerable<TMessage> messages, SynchronizationContext synchronizationContext);
So client has an option whether pass own synchronization context so service will use it when dispatching messages
synchronizationCOntext.Post(clientCallback, ...);
or use it in more straightforward way.
Upvotes: 0
Reputation: 4870
You should provide a complete synchronous API like object Fetch()
and an explicitly marked asynchronous API like void FetchAsnyc(Callback)
. Maybe your client use a different approach to Multitasking then he can implement this with your synchronous API.
And the UI Thread is really not your scope.
Upvotes: 2