/// <summary> /// Queue a new UI task with given delay interval and optional context. /// </summary> /// <param name="task">The task to execute</param> /// <param name="context">The context to pass to the task when it is being called on the UI thread</param> /// <param name="interval">The interval at which to pump the task</param> /// <returns>The cancellable result</returns> public static ICancellableAsyncResult BeginExecuteTask(TaskForUIThread task, object context, TimeSpan interval) { if (!initialized) { throw new InvalidOperationException("UIThreadClassNotInitialized"); } if (task == null) { throw new ArgumentNullException("task"); } TaskData taskData = new TaskData(task, context, interval); if (terminated) { taskData.Cancel(); // can't run it if UI has terminated. return(taskData); } lock (uiTasks) { // add the task uiTasks.Add(taskData); Start(); } return(taskData); }
/// <summary> /// TaskData constructor /// </summary> /// <param name="task">The task to be executed</param> /// <param name="context">The context to pass to the task</param> /// <param name="interval">The interval to call the task at</param> public TaskData(TaskForUIThread task, object context, TimeSpan interval) : this() { Task = task; AsyncState = context; eventHandle = new AutoResetEvent(false); handle = new UiPumpingWaitHandle(this, eventHandle); Interval = interval; }
/// <summary> /// Adds a task to be run on the main UI thread and blocks this thread until the task completes. /// The task is pumped at the given interval /// </summary> public static void ExecuteTask(TaskForUIThread task, object context, TimeSpan interval) { ICancellableAsyncResult result = BeginExecuteTask(task, context, interval); if (!result.IsCompleted) { // block forever waiting! result.AsyncWaitHandle.WaitOne(); } }
/// <summary> /// Adds a task to be run on the main UI thread and blocks this thread until the task completes. /// </summary> public static void ExecuteTask(TaskForUIThread task, object context) { ExecuteTask(task, context, TimeSpan.FromMilliseconds(1)); }