Skip to content

Instantly share code, notes, and snippets.

@Pacane
Created March 19, 2013 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pacane/5196610 to your computer and use it in GitHub Desktop.
Save Pacane/5196610 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Media;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;
using AzgalorStatusCheckerWinform.Properties;
using ServerStatusCrawler;
using Timer = System.Threading.Timer;
namespace AzgalorStatusCheckerWinform
{
public partial class mainForm : Form
{
private ServerState _serverState;
private Timer timer;
public mainForm()
{
InitializeComponent();
statusPictureBox.Image = Resources.unknown;
_serverState = ServerState.UNKNOWN;
Run();
}
private void updateLastTimeCheckedLabel()
{
lblLastTimeCheckedValue.Text = Task.Run(() => DateTime.Now.ToLongTimeString()).Result;
}
private async void checkStatus()
{
var crawler = new AsyncCrawler(new WowServerStatusCrawler("Azgalor", new WowServerStatusParser()));
await UpdatePictureBox(crawler);
await updateCheckedTime();
}
private async Task UpdatePictureBox(AsyncCrawler crawler)
{
bool newState = await crawler.IsServerUp();
await Task.Run(() => setServerState(newState));
}
private void setServerState(bool newState)
{
statusPictureBox.Image = newState ? Resources.online : Resources.offline;
if ((_serverState == ServerState.OFFLINE) && newState)
{
new SoundPlayer(Assembly.GetExecutingAssembly().GetManifestResourceStream
("AzgalorStatusCheckerWinform.leeroy.wav")).Play();
}
_serverState = newState ? ServerState.ONLINE : ServerState.OFFLINE;
}
private async Task updateCheckedTime()
{
await Task.Run(() => updateLastTimeCheckedLabel());
}
private void Run()
{
timer = new Timer(TimerCallback, null, 0, 5000);
}
private void TimerCallback(object state)
{
checkStatus();
}
private enum ServerState
{
ONLINE,
OFFLINE,
UNKNOWN
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment