This is a simple class that runs a timer every 10 seconds to update the title with some information like how long the application has been up, depending on the datetime that is set in another class when the application has finished booting up and is responsive. This application is part of a game server and it just displays how long the server has been up so I know when it was last launched.
In the future it will also include the number of players connected in the title. it is important to continuously reload it as this is a console application and there isn't really much room anywhere else to display such data.
I'm really not sure about the method name CheckServer
but I was feeling very unmotivated at the time, if anyone could think of a better name, be sure to post that too.
I'm looking for basic improvements in overall performance.
internal sealed class ServerProcessWorker : IDisposable
{
private Timer _serverTimer;
private readonly int _timerInterval;
private DateTime _lastUpdate;
private bool _disposing;
public ServerProcessWorker()
{
_timerInterval = 10000;
}
public void Start()
{
_serverTimer = new System.Threading.Timer(e => CheckServer(), null, TimeSpan.Zero, TimeSpan.FromMilliseconds(_timerInterval));
}
private void CheckServer()
{
if ((DateTime.Now - _lastUpdate).TotalMilliseconds <= _timerInterval )
{
return;
}
var serverUptime = DateTime.Now - Sirius.GetServer().GetServerInformation().ServerStarted;
var days = serverUptime.Days + " day" + (serverUptime.Days != 1 ? "s" : "") + ", ";
var hours = serverUptime.Hours + " hour" + (serverUptime.Hours != 1 ? "s" : "") + ", and ";
var minutes = serverUptime.Minutes + " minute" + (serverUptime.Minutes != 1 ? "s" : "");
var uptimeString = days + hours + minutes;
Console.Title = Sirius.GetServer().GetServerInformation().ServerName + " - Uptime: " + uptimeString;
_lastUpdate = DateTime.Now;
}
public void Dispose()
{
if (_disposing)
{
return;
}
_disposing = true;
_serverTimer.Dispose();
_serverTimer = null;
}
}