/// <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> /// <param name="isHighPriority">Whether to attempt to run the action immediately - useful for UI tasks</param> public void AddSync(Action action, string displayInfo, bool isHighPriority) { TaskExecutionPreference executionPreference = TaskExecutionPreference.Fifo; if (isHighPriority) { executionPreference = TaskExecutionPreference.Asap; } Add(action, displayInfo, executionPreference); }
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(); } }