Background:
I have a class "Messenger". He sends messages. But due to restrictions, let's say he can only send messages - no more than - 5 at a time.
I have a WPF application that queues messages if necessary, and waits for a message with a queue to be processed before continuing. Due to the asynchronous nature of the application, any number of messages can be await ed at any given time.
Current implementation:
To do this, I implemented the Task<Result> SendMessage(Message message) API in my message class. Internal to the messaging class is a custom TaskScheduler ( LimitedConcurrencyTaskScheduler from MSDN ), with a concurrency level set to 5. Thus, I will expect that only 5 will be sent regardless of the number of messages in the queue, and my client application will wait patiently until the corresponding message will not be processed.
Problem:
When I await the SendMessage method, I can see through the debugger that the message was completed and the result was returned, but my code is never executed after the await ed method is await !
Are there any special considerations to be made when await task that was scheduled using another TaskScheduler?
Code taken:
From my customer / consumer function:
public async Task Frobulate() { Message myMessage = new Message(x, y, z); await messenger.SendMessage(myMessage);
From my Messenger class:
private TaskScheduler _messengerTaskScheduler = new LimitedConcurrencyLevelTaskScheduler(5); private TaskFactory _messengerTaskFactory = new TaskFactory(_messengerScheduler); public Task<Result> SendMessage(Message message) {
Update:
The βfreezeβ is not actually caused by my custom TaskScheduler ; when I queue Task for the default TaskFactory , the same behavior happens! Something else must be happening on a more fundamental level, probably due to my own stupidity.