public static void CancelUserTask(ClientHandle clientToken) { var l1 = Instance.syncLock; lock (l1) //lock(_instance) { var thandle = Instance.ReadyQueue.FirstOrDefault((th) => th.Token.ID == clientToken.ID); if (thandle != null) // in case task is still in queue only { thandle.task = null; thandle.callback = null; thandle.Token = null; } else // in case theread is running the task - try aborting the thread to cancel the operation (rude behavior) { int itemCount = Instance.ReadyQueue.Count; TaskItem taskItem = null; var l2 = Instance.criticalLock; lock (l2) { taskItem = Instance.Pool.FirstOrDefault(task => task.taskHandle.Token.ID == clientToken.ID); } if (taskItem != null) { lock (taskItem) // only item need the locking { if (taskItem.taskState != TaskState.completed) // double check - in case by the time this lock obtained callback already happened { taskItem.taskState = TaskState.aborted; taskItem.taskHandle.callback = null; // stop callback } if (taskItem.taskState == TaskState.aborted) // this does not need criticalLock { try { taskItem.handler.Abort(); // **** it does not work **** taskItem.handler.Priority = ThreadPriority.BelowNormal; taskItem.handler.IsBackground = true; } catch (Exception e) { logger.Error("", e); } } } } } } }
public static bool Execute(UserTask task, object state, Action <TaskStatus> callback) { ClientHandle ch = CustomThreadPool.Instance.QueueUserTask(task, state, callback); return(true); }
public static bool Execute(UserTask task) { ClientHandle ch = CustomThreadPool.Instance.QueueUserTask(task); return(true); }