private void listTasksWorker(AsyncOperation asyncOp) { Exception e = null; Task[] tasks = null; try { tasks = _syncClient.ListTasks(); } catch (Exception ex) { e = ex; } lock (downloadJobs.SyncRoot) { taskListJobs.Remove(asyncOp.UserSuppliedState); } ListTaskEventArgs args = new ListTaskEventArgs(tasks, e, false, asyncOp.UserSuppliedState); asyncOp.PostOperationCompleted(onListTasksCompletedDelegate, args); }
/// <summary> /// Starts a loop checking the status of active tasks on server /// The loop ends when another thread calls Stop() /// </summary> public void Start(object sender, System.ComponentModel.DoWorkEventArgs e) { Error = null; try { DateTime lastCheckTime = DateTime.UtcNow.AddHours(-1); while (!shouldStop) { bool hasJobs = false; lock (allTasks) { if (allTasks.Count > 0) { hasJobs = true; } } // Time to sleep in seconds int sleepTime = 1; if (hasJobs) { OcrSdkTask[] serverTasks = _restClient.ListTasks(lastCheckTime); lock (allTasks) { foreach (OcrSdkTask task in serverTasks) { //Console.WriteLine( String.Format( "{0}: {1}", task.Id, task.Status ) ); if (allTasks.ContainsKey(task.Id)) { allTasks[task.Id] = task; } } // There is no need to check for statuses of all the tasks on the server - just check // only those that have changed since last call // This trick should work even if local time is incorrect if (allTasks.Count > 0) { lastCheckTime = allTasks.Values.First().StatusChangeTime; } foreach (OcrSdkTask task in allTasks.Values) { if (task.StatusChangeTime > lastCheckTime) { lastCheckTime = task.StatusChangeTime; } } } // Slightly decrease lastCheckTime to avoid losing tasks lastCheckTime = lastCheckTime.AddSeconds(-2); // Find out when to perform next check // TODO } // Sleep for 1 second System.Threading.Thread.Sleep(sleepTime * 1000); } } catch (Exception ex) { Error = ex; } }