Sending 5000 messages using async method in C #

I need to send 10,000 messages. At the moment, this happens synchronously and takes up to 20 minutes to send them to everyone.

// sending messages in a sync way
foreach (var message in messages)
{
    var result = Send(message);
    _logger.Info($"Successfully sent {message.Title}.")
}

To reduce the time it takes to send a message, I would like to use async and wait, but I am worried that C # runtime can handle 15,000 tasks in a workflow.

var tasks = new List<Task>();
foreach (var message in messages) 
{
    tasks.Add(Task.Run(() => Send(message))
}

var t = Task.WhenAll(tasks);
t.Wait();
...

Also, in terms of memory, I'm not sure if it is a good idea to create a list of 15,000 tasks

+7
source share
1 answer

Ever since I got home from work, I played around with it a bit, and here is my answer.

Firstly, it’s Parallel.ForEachvery cool to use, and my 8 cores are very fast.

, 100% , , .

, , .

:

public void MessMessageSender(List<Message> messages)
{
    try
    {
        var parallelOptions = new ParallelOptions();
        _cancelToken = new CancellationTokenSource();
        parallelOptions.CancellationToken = _cancelToken.Token;
        var maxProc = System.Environment.ProcessorCount;
        // this option use around 75% core capacity
        parallelOptions.MaxDegreeOfParallelism = Convert.ToInt32(Math.Ceiling(maxProc * 0.75));
        // the following option use all cores expect 1
        //parallelOptions.MaxDegreeOfParallelism = maxProc - 1;
        try
        {
            Parallel.ForEach(messages, parallelOptions, message =>
            {
                try
                {
                    Send(message);
                    //_logger.Info($"Successfully sent {text.Title}.");
                }
                catch (Exception ex)
                {
                    //_logger.Error($"Something went wrong {ex}.");
                }
            });
        }
        catch (OperationCanceledException e)
        {
            //User has cancelled this request.
        }
    }
    finally
    {
        //What ever dispose of clients;
    }
}

.

:

+7

All Articles