public static void RunSynchronously(Task task) { while (!task.IsCompleted) { if (!ForegroundSynchronizationContext.ProcessAsyncTasks()) { System.Threading.Thread.Sleep(50); } } task.GetAwaiter().GetResult(); }
/// <summary> /// Performs one step of the sync algorithm, which makes at most one service call /// to the Yammer REST endpoint. This method is intended to be called at periodic intervals /// and may return without doing any work, e.g. if the state machine is waiting for a timeout. /// </summary> public void Process() { ForegroundSynchronizationContext.ProcessAsyncTasks(); // Avoid reentrancy that could occur e.g. if the CallingService event handler calls // Utilities.ProcessApplicationEvents(), which causes another timer event to be processed. if (alreadyProcessing) { return; } try { alreadyProcessing = true; // Make sure the previous async task has completed if (previousAsyncTask != null) { bool finished = previousAsyncTask.IsCanceled || previousAsyncTask.IsCompleted || previousAsyncTask.IsFaulted; if (previousAsyncTask.IsFaulted) { var innerException = previousAsyncTask.Exception.InnerExceptions.First(); throw new Exception("A problem occurred while syncing messages.", innerException); } if (!finished) { return; } } previousAsyncTask = ProcessAsync(); } finally { alreadyProcessing = false; } ForegroundSynchronizationContext.ProcessAsyncTasks(); }
public void DeleteFromServer() { var task = DeleteFromServerAsync(); ForegroundSynchronizationContext.RunSynchronously(task); }