private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { try { while(!backgroundWorker1.CancellationPending) { myTask = server.getTask(id); if (myTask != null) { myTask.execute(); server.complete(id, myTask); backgroundWorker1.ReportProgress(0, ++completedTaskCount); } else { Thread.Sleep(1000); } } } catch(SystemException) { MessageBox.Show("Соединение было потеряно."); isLostConnection = true; } }
/** * Return new subtask for execution */ public SubTask getTask(long id) { lock (executionLock) { if (completed) { return(null); } if (processedTasks.ContainsKey(id)) { return(processedTasks[id]); } if (sequences != null && sequences.Count() >= 2) { List <int> seq1; List <int> seq2; seq1 = sequences.First(); sequences.RemoveAt(0); seq2 = sequences.First(); sequences.RemoveAt(0); SubTask task = new SubTask(seq1, seq2); processedTasks.Add(id, task); lastTaskTime = DateTime.Now; return(task); } return(null); // Выполняться никогда не должно (по идее) } }
/** * Transmits the calculation result to the server */ public void complete(long id, SubTask task) { lock (executionLock) { if (processedTasks.ContainsKey(id)) { if (task.haveResult()) { sequences.Add(task.getResult()); processedTasks.Remove(id); if (sequences.Count == 1 && processedTasks.Count == 0) { completed = true; } } } } }
/** * Return new subtask for execution */ public SubTask getTask(long id) { lock (executionLock) { if (completed) return null; if (processedTasks.ContainsKey(id)) { return processedTasks[id]; } if (sequences != null && sequences.Count() >= 2) { List<int> seq1; List<int> seq2; seq1 = sequences.First(); sequences.RemoveAt(0); seq2 = sequences.First(); sequences.RemoveAt(0); SubTask task = new SubTask(seq1, seq2); processedTasks.Add(id, task); lastTaskTime = DateTime.Now; return task; } return null; // Выполняться никогда не должно (по идее) } }