using System; using System.Collections.Generic; using System.Reflection; using System.Text; using System.Threading.Tasks; using TEAMModelOS.SDK.Helper.Common.CollectionHelper; using TEAMModelOS.SDK.DI; using TEAMModelOS.Service.Services.Learn.Interfaces; namespace TEAMModelOS.Service.Services.Learn.Implements { public class TimerWorkService: ITimerWorkService { private readonly AzureCosmosFactory _cosmos; public TimerWorkService(AzureCosmosFactory cosmos) { _cosmos = cosmos; } public void TimerWork(long startTime, int status , Dictionary dict) where T : ID { System.Timers.Timer aTimer = new System.Timers.Timer(); // Create a timer with a two second interval. long time = Math.Abs(startTime - new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds()); aTimer = new System.Timers.Timer(time); // Hook up the Elapsed event for the timer. aTimer.Elapsed += async (sender, e) => await OnTimedEventAsync(_cosmos,status, dict); aTimer.AutoReset = false; aTimer.Enabled = true; } public static async Task OnTimedEventAsync(AzureCosmosFactory _cosmos, int status, Dictionary dict) where T : ID { List homeWorks = await _cosmos.FindByDict(dict); if (homeWorks.IsNotEmpty()) { PropertyInfo propertyInfo = homeWorks[0].GetType().GetProperty("status"); for (int i = 0; i < homeWorks.Count; i++) propertyInfo.SetValue(homeWorks[i], status); await _cosmos.SaveOrUpdateAll(homeWorks); } } } }