void ExecuteActionSync(Action action, string details) { var glueTask = new GlueTask { Action = action, DisplayInfo = details }; lock (mActiveAsyncTasks) { mActiveAsyncTasks.Add(glueTask); } CallTaskAddedOrRemoved(); ((Action)action)(); lock (mActiveAsyncTasks) { mActiveAsyncTasks.Remove(glueTask); } asyncTasks--; // not sure why but this can go into the negative... asyncTasks = System.Math.Max(asyncTasks, 0); CallTaskAddedOrRemoved(); }
public void Add(Action action, string displayInfo, TaskExecutionPreference executionPreference = TaskExecutionPreference.Fifo) { var glueTask = new GlueTask(); glueTask.Action = action; glueTask.DisplayInfo = displayInfo; bool shouldProcess = false; lock (mSyncLockObject) { if (executionPreference == TaskExecutionPreference.Asap) { if (mSyncedActions.Count > 0) { // don't insert at 0, finish the current task, but insert at 1: mSyncedActions.Insert(1, glueTask); } else { mSyncedActions.Add(glueTask); } } else if (executionPreference == TaskExecutionPreference.AddOrMoveToEnd) { var existingAction = mSyncedActions.FirstOrDefault(item => item.DisplayInfo == displayInfo); if (existingAction != null) { // just move it to the end mSyncedActions.Remove(existingAction); mSyncedActions.Add(glueTask); } else { // doesn't exist, so add it normally: mSyncedActions.Add(glueTask); } } else { mSyncedActions.Add(glueTask); } shouldProcess = mSyncedActions.Count == 1 && IsTaskProcessingEnabled; } CallTaskAddedOrRemoved(); if (shouldProcess) { ProcessNextSync(); } }
private void ProcessNextSync() { Action toProcess = null; GlueTask glueTask = null; lock (mSyncLockObject) { if (mSyncedActions.Count > 0) { glueTask = mSyncedActions[0]; toProcess = glueTask.Action; } } if (toProcess != null) { ThreadPool.QueueUserWorkItem(delegate { // This can be uncommented to get information about the task history // to try to improve performance //this.taskHistory.Add(glueTask?.DisplayInfo); toProcess(); bool shouldProcess = false; lock (mSyncLockObject) { // The task may have already been removed if (mSyncedActions.Count > 0 && glueTask == mSyncedActions[0]) { mSyncedActions.RemoveAt(0); } shouldProcess = mSyncedActions.Count > 0 && IsTaskProcessingEnabled; } CallTaskAddedOrRemoved(); if (shouldProcess) { ProcessNextSync(); } }); CallTaskAddedOrRemoved(); } }
/// <summary> /// Adds an action to be executed, guaranteeing that no other actions will be executed at the same time as this. /// Actions added will be executed in the order they were added (fifo). /// </summary> /// <param name="action">The action to execute.</param> /// <param name="displayInfo">The details of the task, to de bisplayed in the tasks window.</param> public void AddSync(Action action, string displayInfo) { bool shouldProcess = false; lock (mSyncLockObject) { var glueTask = new GlueTask(); glueTask.Action = action; glueTask.DisplayInfo = displayInfo; mSyncedActions.Add(glueTask); shouldProcess = mSyncedActions.Count == 1; } CallTaskAddedOrRemoved(); if (shouldProcess) { ProcessNextSync(); } }
void ExecuteActionSync(Action action, string details) { var glueTask = new GlueTask { Action = action, DisplayInfo = details }; lock (mActiveAsyncTasks) { mActiveAsyncTasks.Add(glueTask); } CallTaskAddedOrRemoved(); ((Action)action)(); lock (mActiveAsyncTasks) { mActiveAsyncTasks.Remove(glueTask); } asyncTasks--; CallTaskAddedOrRemoved(); }