private void ProcessTasks(SqlSettings clientSettings)
        {
            var timer = Stopwatch.StartNew();

            var taskList = new List<Task>();
            for (int i = 0; i < 10; ++i)
            {
                int processId = i;
                var task = Task.Factory.StartNew(
                    () => TaskProcess(processId, clientSettings),
                    TaskCreationOptions.LongRunning);
                taskList.Add(task);
            }

            while (taskList.Count > 0)
            {
                for (int i = taskList.Count - 1; i >= 0; i--)
                {
                    var task = taskList[i];
                    if (task.IsCanceled)
                    {
                        Console.WriteLine("Task canceled!");
                        taskList.RemoveAt(i);
                    }
                    if (task.IsFaulted)
                    {
                        Console.WriteLine("Task faulted!");

                        taskList.RemoveAt(i);
                    }

                    if (task.IsCompleted)
                    {
                        taskList.RemoveAt(i);
                    }

                    if (Identities.Count > 0)
                    {
                        Guid identity;
                        if (Identities.TryDequeue(out identity))
                        {
                            var messengerClient = new Core.Services.Messenger(clientSettings, new MsSqlRepositoryFactory(clientSettings));
                            if (messengerClient.Cancel(identity))
                            {
                                Console.WriteLine("Canceled {0}", identity);
                            }
                        }
                    }
                }
            }

            timer.Stop();

            Console.WriteLine("All tasks finished. Time: " + timer.Elapsed);
        }
        public void Execute(SqlSettings clientSettings)
        {
            var random = new Random();
            var finished = false;
            while (!finished)
            {
                var key = Console.ReadKey(true);
                if (key.Key == ConsoleKey.Escape)
                {
                    finished = true;
                }
                else
                {
                    var messengerClient = new Core.Services.Messenger(clientSettings, new MsSqlRepositoryFactory(clientSettings));
                    //var messengerClient = new Core.Services.Messenger(clientSettings, new PostgreSqlRepositoryFactory(clientSettings));

                    int delayExecution = 0;
                    Guid? identity = null;
                    int queueIndex;
                    if (key.KeyChar >= '0' && key.KeyChar <= '9')
                    {
                        queueIndex = (key.KeyChar-'0');
                        delayExecution = 30;
                        identity = Guid.NewGuid();
                        Identities.Enqueue(identity.Value);
                    }
                    else if (key.Key == ConsoleKey.Delete)
                    {
                        var identityToCancel = Identities.Dequeue();
                        var canceled = messengerClient.Cancel(identityToCancel);
                        if (canceled)
                        {
                            Console.WriteLine("Canceled: {0}", identityToCancel);
                        }
                        else
                        {
                            Console.WriteLine("Unable to cancel anymore: {0}", identityToCancel);
                        }
                        continue;
                    }
                    else
                    {
                        queueIndex = random.Next(0, Queues.Length);
                    }

                    var queueName = Queues[queueIndex];

                    var publishSettings = new PublishSettings
                    {
                        DelayExecutionInSeconds = delayExecution,
                        Identity = identity
                    };

                    var request = new FatQueuePrintMessageEvent
                    {
                        Message = new CustomMessage { Message = "Published from main manually. Delay: " + delayExecution }
                    };

                    messengerClient.Publish<FatQueuePrintMessageEventHandler>(handler => handler.Handle(request), new QueueName(queueName), publishSettings);

                    Console.WriteLine("Message published to queue {0}", queueName);
                }
            }
        }