Channel Writer requiring Task.Delay and not processing all messages
Solution 1:
Task.WhenAll(Task.Run(() => StartListener()));
StartListener
returns a Task
. You wrap that in Task.Run
, starting another thread to run that task. You then pass than task to the Task.WhenAll
method, which returns a Task
that you promptly throw away.
The only tasks you add to the taskList
variable are the StartJob
tasks. Your Main
method will finish as soon as all of the StartJob
tasks have finished. It will not wait for the StartListener
task to finish.
Change your code to wait for the listener to finish.
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
taskList.Add(Task.Run(() => StartListener()));
for (int i = 0; i < 10; i++)
{
int task = i;
taskList.Add(Task.Run(() => StartJob(task)));
}
Task.WaitAll(taskList.ToArray());
sw.Stop();
Console.WriteLine("Total Messages Processed: {0} in time {1} MessageListCount {2}",
midpointCount, sw.Elapsed, messageList.Reader.Count);
}