52 lines
1.4 KiB
C#
52 lines
1.4 KiB
C#
using System;
|
|
using System.Timers;
|
|
|
|
namespace Demka_Snova_1.Hardik.Date
|
|
{
|
|
public class Otchot
|
|
{
|
|
private Timer _timer;
|
|
private int _sessionDuration = 10;
|
|
private int _warningTime = 5;
|
|
private int _blockTime = 3;
|
|
private DateTime _sessionStartTime;
|
|
|
|
public event Action<string> OnWarning;
|
|
public event Action OnSessionEnd;
|
|
|
|
public void Start()
|
|
{
|
|
_sessionStartTime = DateTime.Now;
|
|
_timer = new Timer(60000); // 1 минута
|
|
_timer.Elapsed += TimerOtchot;
|
|
_timer.Start();
|
|
}
|
|
|
|
private void TimerOtchot(object sender, ElapsedEventArgs e)
|
|
{
|
|
var OtchotTime = DateTime.Now - _sessionStartTime;
|
|
var remainingTime = _sessionDuration - OtchotTime.TotalMinutes;
|
|
|
|
if (remainingTime <= _warningTime && remainingTime > 0)
|
|
{
|
|
OnWarning?.Invoke($"До окончания сеанса осталось {remainingTime} минут.");
|
|
}
|
|
|
|
if (remainingTime <= 0)
|
|
{
|
|
_timer.Stop();
|
|
OnSessionEnd?.Invoke();
|
|
BlockLogin();
|
|
}
|
|
}
|
|
|
|
private void BlockLogin()
|
|
{
|
|
|
|
var blockTime = DateTime.Now.AddMinutes(_blockTime);
|
|
while (DateTime.Now < blockTime) // Блок на 3 минуты
|
|
{
|
|
}
|
|
}
|
|
}
|
|
} |