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); }
public uint AddShadulerTask(Action <ShadulerTask> configurate) { ShadulerTask forAdd = new ShadulerTask(GetNextID()); configurate(forAdd); AddShadulerTask(forAdd); if (IsFirst(forAdd)) { Reconfig(); } return(forAdd.id); }
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); }
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 } }
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 ); }
private bool IsFirst(ShadulerTask task) { return(shadulerTasks.First.Value.Equals(task)); }