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!

Cinchoo – Abortable Queued Execution Service

ChoAbortableQueuedExecutionService

In this article, we will go over the one of service provided by Cinchoo framework, ChoAbortableQueuedExecutionService. 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 and those are abortable anytime. 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 AbortableQueuedExectionService (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 AbortableQueuedExecutionService:

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

static void Main(string[] args)
{
    IChoAbortableAsyncResult 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 AbortableQueuedExecutionService:

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

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

    IChoAbortableAsyncResult 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
}

Aborting Operation:

Any operation queued to this service can be aborted anytime. In here, I’ll show you how we can abort a operation after queue the request to the service.

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

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

    Thread.Sleep(1000);
    result.Abort(); //Abort the operation
}

Try for yourself!