Inheritance: Task, ILeveledTask
Exemple #1
0
        public void RemoveQueue(string QueueId)
        {
            LeveledTask lt = new LeveledTask(() =>
            {
                // this will remove the Q from the list of Q
                // but will not null it so if we have an going exection it will just keep on going.
                // because the list of Q and locks no longer maintain a reference to lQ & HQ and lock
                // it will evantually be null

                Trace.WriteLine(string.Format("queue {0} will be removed", QueueId), "info");

                ConcurrentQueue <Task> q;
                object oLock;


                m_HiQueues.TryRemove(QueueId, out q);
                m_LowQueues.TryRemove(QueueId, out q);

                MasterQueueLocks.TryRemove(QueueId, out oLock);
            });

            lt.IsHighPriority = false;
            lt.QueueId        = QueueId;
            lt.Start(this);
        }
        public void AsyncAsync()
        {
            var maxQueue = 10;
            var maxTasks = 5000;
            var maxSleep = 10;
            var IsHighPrority = false;
            var result = new ConcurrentDictionary<string, ConcurrentDictionary<string, long>>();
            var sw = new Stopwatch();
            sw.Start();

            var Tasks = new List<Task>();

            for (var qNum = 1; qNum <=  maxQueue; qNum++)
            {
                var qName = string.Concat("Q", qNum);
                for (var Seq = 1; Seq < maxTasks / maxQueue; Seq++)
                {
                    var taskP = new TaskParameters() { Q = qName, Seq = Seq };

                    LeveledTask lt = new LeveledTask(async (param) =>
                    {
                        // this force the new task not to be inlined.

                        await Task.Factory.StartNew(async () => { await Task.Delay(100); }, TaskCreationOptions.LongRunning );
                        var p = param as TaskParameters;

                        var resultQ = result.GetOrAdd(p.Q, new ConcurrentDictionary<string, long>());
                        resultQ.AddOrUpdate(string.Concat("S", p.Seq), DateTime.UtcNow.Ticks, (key, current) => { throw new InvalidOperationException("Found existing key"); });
                        //Trace.WriteLine(string.Format("Task {0} on Queue {1} - Start", p.Seq, p.Q), "info");
                        Thread.Sleep(maxSleep);
                        //Trace.WriteLine(string.Format("Task {0} on Queue {1} - End", p.Seq, p.Q), "info");
                        resultQ.AddOrUpdate(string.Concat("E", p.Seq), DateTime.UtcNow.Ticks, (key, current) => { throw new InvalidOperationException("Found existing key"); });

                    },
                    taskP);

                    lt.QueueId = qName;
                    lt.IsHighPriority = IsHighPrority;
                    lt.Start(scheduler);
                    Tasks.Add(lt);
                }
            }

            Task.WhenAll(Tasks).Wait();
            sw.Stop();
            Trace.WriteLine(string.Format("{0} Tasks on {1} Queues - Executed in {2} sec", maxTasks, maxQueue, sw.Elapsed.Seconds), "info");
            //verify(ref result);

            // tasks that uses normal start.run will lose order in the queue.
            // the only way that tasks maintain order is to by using
            // leveled tasks to create sub tasks.
        }
        private LeveledTask addTask(ref ConcurrentDictionary<string, ConcurrentDictionary<string, long>>  allresult , 
                            TaskParameters taskP, 
                            string q,
                            int maxSleep, 
                            bool IsHighPrority = false)
        {
            var result = allresult;

            LeveledTask lt = new LeveledTask((param) =>
            {
                var p = param as TaskParameters;

                var resultQ = result.GetOrAdd(p.Q, new ConcurrentDictionary<string, long>());
                resultQ.AddOrUpdate(string.Concat("S", p.Seq), DateTime.UtcNow.Ticks, (key, current) => { throw new InvalidOperationException("Found existing key"); });
                //Trace.WriteLine(string.Format("Task {0} on Queue {1} - Start", p.Seq, p.Q), "info");
                Thread.Sleep(maxSleep);
                //Trace.WriteLine(string.Format("Task {0} on Queue {1} - End", p.Seq, p.Q), "info");
                resultQ.AddOrUpdate(string.Concat("E", p.Seq), DateTime.UtcNow.Ticks, (key, current) => { throw new InvalidOperationException("Found existing key"); });
            },
                       taskP);

            lt.QueueId = q;
            lt.IsHighPriority = IsHighPrority;
            lt.Start(scheduler);

            return lt;
        }
        public void Task_T()
        {
            var maxQueue = 10;
            var maxTasks = 5000;
            var maxSleep = 10;
            var IsHighPrority = false;
            var result = new ConcurrentDictionary<string, ConcurrentDictionary<string, long>>();
            var sw = new Stopwatch();
            sw.Start();

            var Tasks = new List<Task<long>>();

            for (var qNum = 1; qNum <= maxQueue; qNum++)
            {
                var qName = string.Concat("Q", qNum);
                for (var Seq = 1; Seq < maxTasks / maxQueue; Seq++)
                {
                    var taskP = new TaskParameters() { Q = qName, Seq = Seq };

                    LeveledTask<long> lt = new LeveledTask<long>( (param) =>
                    {
                        // this force the new task not to be inlined.

                        var p = param as TaskParameters;

                        var resultQ = result.GetOrAdd(p.Q, new ConcurrentDictionary<string, long>());
                        resultQ.AddOrUpdate(string.Concat("S", p.Seq), DateTime.UtcNow.Ticks, (key, current) => { throw new InvalidOperationException("Found existing key"); });
                        //Trace.WriteLine(string.Format("Task {0} on Queue {1} - Start", p.Seq, p.Q), "info");
                        Thread.Sleep(maxSleep);
                        //Trace.WriteLine(string.Format("Task {0} on Queue {1} - End", p.Seq, p.Q), "info");
                        resultQ.AddOrUpdate(string.Concat("E", p.Seq), DateTime.UtcNow.Ticks, (key, current) => { throw new InvalidOperationException("Found existing key"); });

                        return DateTime.Now.Ticks;
                    },
                    taskP);

                    lt.QueueId = qName;
                    lt.IsHighPriority = IsHighPrority;
                    lt.Start(scheduler);
                    Tasks.Add(lt);
                }
            }

            Task.WhenAll(Tasks).Wait();

            foreach (var task in Tasks)
                Trace.WriteLine(String.Format("Task result was:{0} ", task.Result));

            sw.Stop();
            Trace.WriteLine(string.Format("{0} Tasks on {1} Queues - Executed in {2} sec", maxTasks, maxQueue, sw.Elapsed.Seconds), "info");
            verify(ref result);
        }
        public void RemoveQueue(string QueueId)
        {
            LeveledTask lt = new LeveledTask(() =>
            {

                // this will remove the Q from the list of Q
                // but will not null it so if we have an going exection it will just keep on going.
                // because the list of Q and locks no longer maintain a reference to lQ & HQ and lock
                // it will evantually be null

                Trace.WriteLine(string.Format("queue {0} will be removed", QueueId), "info");

                ConcurrentQueue<Task> q;
                object oLock;

                m_HiQueues.TryRemove(QueueId, out q);
                m_LowQueues.TryRemove(QueueId, out q);

                MasterQueueLocks.TryRemove(QueueId, out oLock);

            });

            lt.IsHighPriority = false;
            lt.QueueId = QueueId;
            lt.Start(this);
        }