/// <summary> /// To be tied to PatientsSorted. Prints changes to console and to file "fileName". Also 'ticks' the cancellation-token if there are no patients present /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private void OnPatientsSorted(object sender, PatientsSortedEventArgs args) { string toWrite = $"{args.PatientsMovedToICU} patients was moved to ICU, which now has {args.PatientsInICU} patients.\n" + $"{args.PatientsMovedToSanatorium} patients was moved to the Sanatorium, which now has {args.PatientsInSanatorium} patients.\n"; lock (this) { File.AppendAllText(fileName, toWrite); } Console.Write(toWrite); if (args.PatientsInSanatorium == 0 && args.PatientsInICU == 0) { args.CancellationRequested = true; } }
/// <summary> /// Simulates filling up the ICU and Sanatorium with patients according to symptom-level and patients age. Throws event PatientsSorted when done and /// if there are no more patients in neither the sanatorium or the queue, sets the cancellationToken to true /// </summary> public void SimulatePatientSorting() { PatientsSortedEventArgs args = new PatientsSortedEventArgs(); while (!args.CancellationRequested) { //Reset the args values args.PatientsMovedToSanatorium = 0; args.PatientsMovedToICU = 0; Thread.Sleep(5000); using (var hospitalContext = new HospitalContext()) { var patientsInICU = (from patient in hospitalContext.Patients where patient.Status == PatientStatus.ICU select patient).ToList(); args.PatientsInICU = patientsInICU.Count; //Check if ICU is full, otherwise, fill it if (patientsInICU.Count < maxICU) { var patientsToMove = (from patient in hospitalContext.Patients where patient.Status == PatientStatus.Queue || patient.Status == PatientStatus.Sanatorium orderby patient.SymptomLevel descending, patient.BirthDate ascending select patient).Take(maxICU - patientsInICU.Count).ToList(); //Change status of the patients provided, to ICU for (int i = 0; i < patientsToMove.Count; i++) { patientsToMove[i].Status = PatientStatus.ICU; } args.PatientsMovedToICU = patientsToMove.Count; args.PatientsInICU += patientsToMove.Count; } hospitalContext.SaveChanges(); var patientsInSanatorium = (from patient in hospitalContext.Patients where patient.Status == PatientStatus.Sanatorium select patient).ToList(); args.PatientsInSanatorium = patientsInSanatorium.Count; //Check if Sanatorium is full, otherwise, fill it if (patientsInSanatorium.Count < maxSanatorium) { var patientsToMove = (from patient in hospitalContext.Patients where patient.Status == PatientStatus.Queue orderby patient.SymptomLevel descending, patient.BirthDate ascending select patient).Take(maxSanatorium - patientsInSanatorium.Count).ToList(); //Change status of the patients provided, to Sanatorium for (int i = 0; i < patientsToMove.Count; i++) { patientsToMove[i].Status = PatientStatus.Sanatorium; } args.PatientsMovedToSanatorium = patientsToMove.Count; args.PatientsInSanatorium += patientsToMove.Count; } hospitalContext.SaveChanges(); } PatientsSorted?.Invoke(this, args); //If there are no patients left, stop the thread if (args.PatientsInICU == 0 && args.PatientsInSanatorium == 0) { break; } } }