Created
March 19, 2013 14:31
-
-
Save Pacane/5196610 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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