Example #1
0
        /// <summary>
        /// start a timer for each user
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        private CustomTimer StartTimer(User user)
        {
            string name = user.FirstName + " " + user.LastName;
            //note - for demo reasons - the timers interval is in seconds - not it minutes!
            var timer = new CustomTimer
            {
                id       = user.UserId,
                name     = name,
                Interval = 1000 * user.TaskInterval,
            };

            _logger.LogInformation($"start timer for user {name}");
            timer.Elapsed += timer_Elapsed;
            timer.Start();
            return(timer);
        }
Example #2
0
        /// <summary>
        /// manage a list of timeres for all users with "SendTasks = true"
        /// eache timer should send do some task (like sending mail to the user)
        /// </summary>
        /// <returns>number of active </returns>
        public int ManageUsers()
        {
            _logger.LogInformation("ManageUsers");
            int res;

            _timersList = new List <CustomTimer> {
            };
            using (var scope = _scopeFactory.CreateScope())
            {
                var db       = scope.ServiceProvider.GetRequiredService <supercomDbContext>();
                var UserList = db.Users.Where(p => p.SendTasks == true).ToList();
                foreach (User user in UserList)
                {
                    CustomTimer timer = StartTimer(user);
                    _timersList.Add(timer);
                }
                //db.Users.UpdateRange(UserList);
                //db.SaveChanges();
                res = UserList.Count;
            }
            return(res);
        }