/// <summary> /// Starting a delayed task. /// </summary> /// <param name="delayedAction">The delayed action.</param> /// <param name="waitTime">The timespan before invoking the action. (Milliseconds.)</param> /// <param name="repeat">The amount of times the action should be repeated.</param> public static uint StartDelayedTask(ThreadAction delayedAction, int waitTime, int repeat = 0) { DelayedAction taskAction = new DelayedAction(); lock (SyncRoot) { taskAction.actionID = TaskID; TaskID++; } taskAction.allowedTime = DateTime.Now.AddMilliseconds(waitTime); taskAction.repeat = repeat; taskAction.threadAction = new ThreadAction(() => { if (DateTime.Now >= taskAction.allowedTime) { delayedAction.Invoke(); if (taskAction.repeated >= taskAction.repeat) { DelayedAction outDA; taskObjects.TryRemove(taskAction.actionID, out outDA); } taskAction.repeated++; } }); taskObjects.TryAdd(taskAction.actionID, taskAction); return taskAction.actionID; }
/// <summary> /// Starting a delayed task. /// </summary> /// <param name="delayedAction">The delayed action.</param> /// <param name="waitTime">The timespan before invoking the action. (Milliseconds.)</param> /// <param name="repeat">The amount of times the action should be repeated.</param> public static uint StartDelayedTask(ThreadAction delayedAction, int waitTime, int repeat = 0) { DelayedAction taskAction = new DelayedAction(); lock (SyncRoot) { taskAction.actionID = TaskID; TaskID++; } taskAction.allowedTime = DateTime.Now.AddMilliseconds(waitTime); taskAction.repeat = repeat; taskAction.threadAction = new ThreadAction(() => { if (DateTime.Now >= taskAction.allowedTime) { delayedAction.Invoke(); if (taskAction.repeated >= taskAction.repeat) { DelayedAction outDA; taskObjects.TryRemove(taskAction.actionID, out outDA); } taskAction.repeated++; } }); taskObjects.TryAdd(taskAction.actionID, taskAction); return(taskAction.actionID); }
public ThreadAsync DoSomething(ThreadAction action, object obj = null) { ThreadAsync async = null; Thread thread = new Thread(() => { if (action != null) { ThreadAction temp = action; action = null; string error = temp.Invoke(obj); async.SetError(error); async.SetIsDone(true); } }); async = new ThreadAsync(thread); thread.Start(); return(async); }