Cinchoo – Queued Execution Service

ChoQueuedExecutionService

In this article, we will go over the one of service provided by Cinchoo framework, ChoQueuedExecutionService. This service used to execute series of methods in orderly fashion. Each operation pushed to this service will be executed in first come first serve basis. Also we can instruct to run each operation with Timeout (ms), Number of retries (in case of error while running), sleep between retries parameters.

Either you can use Global QueuedExectionService (application level service) or instantiate new one locally and use them.

1. Add reference to Cinchoo.Core.dll assembly

2. Namespace Cinchoo.Core

Using Global QueuedExecutionService:

Below sample, inject a delegate to Global execution service and wait for the call to complete.

static void Main(string[] args)
{
    IChoAsyncResult result = ChoQueuedExecutionService.Global.Enqueue(() =>
        {
            Thread.Sleep(5000);
            Console.WriteLine("Test Message");
        });

    result.EndInvoke(); //Optional call, to get the result or wait for the call to finish
}

Creating and using local QueuedExecutionService:

In below sample, we create a local QueuedExecutionService and injecting method to execute.

static void Main(string[] args)
{
    ChoQueuedExecutionService service = new ChoQueuedExecutionService("TestQService", false);

    IChoAsyncResult result = service.Enqueue(() =>
        {
            Thread.Sleep(5000);
            Console.WriteLine("Test Message");
        });

    result.EndInvoke(); //Optional call, to get the result or wait for the call to finish
}

Try for yourself!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s