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!

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