コード例 #1
0
ファイル: Timers.cs プロジェクト: gris-martin/TeamoSharp
        public Timers(double updateInterval, TeamoEntry entry, TeamoContext context, IClientService clientService, ILogger logger)
        {
            _context       = context;
            _clientService = clientService;
            _entry         = entry;
            _logger        = logger;

            _semaphore = new System.Threading.SemaphoreSlim(1, 1);
            bool isUpdating = false;
            var  entryId    = _entry.Id.Value;

            // Update timer
            _updateTimer = new Timer
            {
                Interval  = updateInterval,
                AutoReset = true
            };
            _updateTimer.Elapsed += async(sender, e) =>
            {
                if (isUpdating)
                {
                    return;
                }
                Console.WriteLine($"[TIMER] Updating {entryId}");
                await ExclusiveAsync(async() =>
                {
                    var dbEntry = _context.GetEntry(entryId);
                    await _clientService.UpdateMessageAsync(dbEntry);
                });

                Console.WriteLine($"[TIMER] Update done {entryId}");
            };


            // Start timer
            _startTimer = new Timer
            {
                AutoReset = false
            };
            _startTimer.Elapsed += async(sender, e) =>
            {
                Console.WriteLine($"[TIMER] Starting finish {entryId}");
                _updateTimer.Stop();
                await ExclusiveAsync(async() =>
                {
                    _logger.LogInformation($"Creating start message for entry {entryId}");
                    var dbEntry = _context.GetEntry(entryId);
                    await _clientService.DeleteMessageAsync(entry.Message);
                    await _clientService.CreateStartMessageAsync(dbEntry);
                    await _context.DeleteAsync(entry.Id.Value);
                    EventHandler <ElapsedEventArgs> handler = TimerFinished;
                    handler?.Invoke(sender, e);
                    _logger.LogInformation($"Successfully created start message for {entryId}");
                });

                Console.WriteLine($"[TIMER] Finished {entryId}");
            };
        }
コード例 #2
0
        public async Task <ClientMessage> CreateMessageAsync(TeamoEntry entry)
        {
            _logger.LogInformation($"Creating client message for entry {entry.Id}");

            var messageWithId = new ClientMessage
            {
                ServerId  = entry.Message.ServerId,
                ChannelId = entry.Message.ChannelId,
                MessageId = _id.ToString()
            };

            _id++;

            await Task.Delay(1000);

            return(messageWithId);
        }
コード例 #3
0
 public async Task UpdateMessageAsync(TeamoEntry entry)
 {
     _logger.LogInformation($"Updating message for entry {entry.Id}");
     await Task.Delay(1000);
 }
コード例 #4
0
 public async Task CreateStartMessageAsync(TeamoEntry entry)
 {
     _logger.LogInformation($"Creating start message for entry {entry.Id}");
     await Task.Delay(1000);
 }