❔ Timer

Hi, I'm trying to write a custom timer. I can't figure out how I can write an event that triggers when the time to the end of the interval changes. I would like the remaining time in milliseconds to be written to a file every second.
No description
8 Replies
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
zechosenpessimist
zechosenpessimistOP14mo ago
class Program
{
static void Main()
{
SpecialTimer specialTimer = SpecialTimer.Instance;

specialTimer.TimeLeftChanged += TimeLeftChangedHandler;

specialTimer.AddTime(10);

Console.ReadLine();
}

private static void TimeLeftChangedHandler(int secondsLeft)
{
Console.WriteLine($"Time left: {secondsLeft} seconds");
}

public class SpecialTimer : System.Timers.Timer
{
private static SpecialTimer instance;
private static readonly object lockObject = new object();

public delegate void TimeLeftChangedHandler(int secondsLeft);

public event TimeLeftChangedHandler? TimeLeftChanged;

private SpecialTimer() : base()
{
Elapsed += TimerElapsed;
}

private void TimerElapsed(object sender, ElapsedEventArgs e)
{
Stop();
}

public void AddTime(int seconds)
{
if (Enabled && Interval > 0)
{
Stop();

double newInterval = Interval + seconds * 1000;
Interval = newInterval > 0 ? newInterval : 0;

Start();
}
else
{
instance = new SpecialTimer();
instance.Interval = seconds * 1000;
instance.Start();
}
}

public static SpecialTimer Instance
{
get
{
lock (lockObject)
{
return instance ?? (instance = new SpecialTimer());
}
}
}
}
}
class Program
{
static void Main()
{
SpecialTimer specialTimer = SpecialTimer.Instance;

specialTimer.TimeLeftChanged += TimeLeftChangedHandler;

specialTimer.AddTime(10);

Console.ReadLine();
}

private static void TimeLeftChangedHandler(int secondsLeft)
{
Console.WriteLine($"Time left: {secondsLeft} seconds");
}

public class SpecialTimer : System.Timers.Timer
{
private static SpecialTimer instance;
private static readonly object lockObject = new object();

public delegate void TimeLeftChangedHandler(int secondsLeft);

public event TimeLeftChangedHandler? TimeLeftChanged;

private SpecialTimer() : base()
{
Elapsed += TimerElapsed;
}

private void TimerElapsed(object sender, ElapsedEventArgs e)
{
Stop();
}

public void AddTime(int seconds)
{
if (Enabled && Interval > 0)
{
Stop();

double newInterval = Interval + seconds * 1000;
Interval = newInterval > 0 ? newInterval : 0;

Start();
}
else
{
instance = new SpecialTimer();
instance.Interval = seconds * 1000;
instance.Start();
}
}

public static SpecialTimer Instance
{
get
{
lock (lockObject)
{
return instance ?? (instance = new SpecialTimer());
}
}
}
}
}
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
zechosenpessimist
zechosenpessimistOP14mo ago
Yes, I'm trying to write an event that triggers when the time on the timer changes
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
zechosenpessimist
zechosenpessimistOP14mo ago
time remaining until the end of the timer
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server