예제 #1
0
        /// <summary>
        /// To be tied to PatientsRecoveringOrDying. Logs event-changes. also checks if there are any patients left. if not, 'ticks' the cancellationToken
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnPatientsDyingOrRecovering(object sender, PatientsRecoveredOrDiedEventArgs args)
        {
            string toWrite = $"{args.PatientsRecovered} just recovered and {args.PatientsDied} died.\n";

            lock (this)
            {
                File.AppendAllText(fileName, toWrite);
            }
            Console.Write(toWrite);
            if (args.PatientsLeft == 0)
            {
                args.CancellationRequested = true;
            }
        }
예제 #2
0
        /// <summary>
        /// Checks all the patients in the hospital, and if they have recovered or died, switches their statuses
        /// </summary>
        public void SimulatePatientRecoveringOrDying()
        {
            PatientsRecoveredOrDiedEventArgs args = new PatientsRecoveredOrDiedEventArgs();

            while (!args.CancellationRequested)
            {
                Thread.Sleep(5000);
                //Reset args-values
                args.PatientsRecovered = 0;
                args.PatientsDied      = 0;
                args.PatientsLeft      = 0;
                using (var hospitalContext = new HospitalContext())
                {
                    var patients = (from patient in hospitalContext.Patients
                                    where patient.Status != PatientStatus.Afterlife && patient.Status != PatientStatus.Recovered
                                    select patient).ToList();
                    for (int i = 0; i < patients.Count; i++)
                    {
                        if (patients[i].SymptomLevel < 1)
                        {
                            patients[i].Status      = PatientStatus.Recovered;
                            args.PatientsRecovered += 1;
                        }
                        else if (patients[i].SymptomLevel > 9)
                        {
                            patients[i].Status = PatientStatus.Afterlife;
                            args.PatientsDied += 1;
                        }
                        else
                        {
                            args.PatientsLeft += 1;
                        }
                    }

                    hospitalContext.SaveChanges();
                }
                PatientsRecoveredOrDied?.Invoke(this, args);
            }
        }