Skip to content

eyupgevenim/Simple.NETCore.QueueManager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple .Net Core Queue(producer-consumer) Manager

Simple.NETCore.QueueManager is a basic producer-consumer model that uses dataflow. for .Net Core

.net core producer-consumer sample:
The Produce method calls the Post method in a loop to synchronously write data to the target block. After the Produce method writes all data to the target block, it calls the Complete method to indicate that the block will never have additional data available. The Consume method uses the async and await operators to asynchronously compute the T data type that are received from the BlockingCollection object.

Sample

static void Main(string[] args)
{
    Func<int, string> producer = (index) =>
    {
        System.Threading.Thread.Sleep(1500);
        var todoSome = $"index:{index}";
        Console.WriteLine($" + producer => {todoSome}");
        return todoSome;
    };

    Action<string> consumer = (result) =>
    {
        System.Threading.Thread.Sleep(1000);
        Console.WriteLine($" - consumered => {result}");
    };

    var queueConfigurations = new QueueConfigurations(3, 1);
    var queueService = new QueueService<string>(queueConfigurations);
    queueService.StartProcess(100, producer, consumer);

    foreach (var ex in queueService.Exceptions)
        Console.WriteLine($"Exception-> Method:{ex.Method}|Message:{ex.Message}");

}

output:

producer consumer


Resource

How to: Implement a Producer-Consumer Dataflow Pattern

BlockingCollection<T>