Example #1
0
        private uint AddShadulerTask(ShadulerTask forAdd)
        {
            forAdd.unixTimestamp = GetUnixTimestamp() + forAdd.delay;

            //25    35
            //   30    40
            LockOperations((LinkedList <ShadulerTask> obj) => {
                LinkedListNode <ShadulerTask> node = obj.First;
                bool isFirst = node == null;

                while (node != null && node.Value.unixTimestamp < forAdd.unixTimestamp)
                {
                    node = node.Next;
                }

                if (node == null)
                {
                    if (isFirst)
                    {
                        obj.AddFirst(forAdd);
                    }
                    else
                    {
                        obj.AddLast(forAdd);
                    }
                }
                else
                {
                    obj.AddBefore(node, forAdd);
                }
            });

            return(forAdd.id);
        }
Example #2
0
        public uint AddShadulerTask(Action <ShadulerTask> configurate)
        {
            ShadulerTask forAdd = new ShadulerTask(GetNextID());

            configurate(forAdd);

            AddShadulerTask(forAdd);

            if (IsFirst(forAdd))
            {
                Reconfig();
            }

            return(forAdd.id);
        }
Example #3
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }
            if (!(obj is ShadulerTask))
            {
                return(false);
            }

            ShadulerTask other = obj as ShadulerTask;

            if (other == this)
            {
                return(true);
            }

            return(id == other.id);
        }
Example #4
0
        private void Reconfig()
        {
            timer.Change(Timeout.Infinite, Timeout.Infinite);

            ShadulerTask forExecute = null;

            LockOperations((obj) => {
                forExecute = shadulerTasks.First.Value;
            });

            if (forExecute != null)
            {
                //get next
                long delay = forExecute.unixTimestamp - GetUnixTimestamp();
                timer.Change(delay, Timeout.Infinite);
            }
            else
            {
                //stop
            }
        }
Example #5
0
        public Shaduler()
        {
            timer = new Timer(
                (state) => {
                ShadulerTask forExecute = null;
                LockOperations((obj) => {
                    forExecute = obj.First.Value;
                    obj.RemoveFirst();
                });

                ThreadPool.QueueUserWorkItem((stateTP) => { forExecute.action(); });
                if (forExecute.loop)
                {
                    AddShadulerTask(forExecute);
                }
                Reconfig();
            },
                null,
                Timeout.Infinite,                //dueTime
                Timeout.Infinite                 //period
                );
        }
Example #6
0
 private bool IsFirst(ShadulerTask task)
 {
     return(shadulerTasks.First.Value.Equals(task));
 }