Skip to content

Commit

Permalink
More adjustments on the re-occuring task
Browse files Browse the repository at this point in the history
  • Loading branch information
zokker13 committed Feb 14, 2018
1 parent 8b0dbd4 commit 3c6d4e2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
2 changes: 1 addition & 1 deletion RatherWeird/RatherWeird/OnlinePlayers.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:RatherWeird"
mc:Ignorable="d"
Title="OnlinePlayers" Height="426.89" Width="296.639" Loaded="Window_Loaded" Unloaded="Window_Unloaded" Closing="Window_Closing">
Title="OnlinePlayers" Height="426.89" Width="296.639" Closing="Window_Closing">
<Grid>
<local:Ra3Playerlist x:Name="ra3Players"/>
</Grid>
Expand Down
32 changes: 21 additions & 11 deletions RatherWeird/RatherWeird/OnlinePlayers.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,9 @@ public OnlinePlayers()
InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
ra3Players.StartReoccuringTask();
}

private void Window_Unloaded(object sender, RoutedEventArgs e)
{
ra3Players.StopReoccuringTask();
}

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (_stopClosing)
if (_stopClosing)
{
e.Cancel = true;
Hide();
Expand All @@ -52,5 +42,25 @@ public void ProperlyClose()

Close();
}

public new void Hide()
{
ra3Players.StopReoccuringTask();
base.Hide();
}

public new void Show()
{
if (Visibility == Visibility.Visible)
{
Activate();
}
else
{
ra3Players.StartReoccuringTask();
base.Show();
}

}
}
}
33 changes: 24 additions & 9 deletions RatherWeird/RatherWeird/Ra3Playerlist.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,33 @@ public Ra3Playerlist()

public void StartReoccuringTask()
{
Logger.Info("Starting Reoccuring task");
_tokenSource = new CancellationTokenSource();

async void Repeated(Task _)
if (_tokenSource != null && !_tokenSource.IsCancellationRequested)
{
Logger.Debug("Attempting to download and insert player statistics");
CncGeneralInfo info = await GatherData();
InsertData(info);
await Task.Delay(1000 * 60, _tokenSource.Token).ContinueWith(Repeated, _tokenSource.Token);
// previous task was *NOT* cancelled so we just go home here
Logger.Debug("Attempted to start a second task but that was supressed (misuse and such)");
return;
}

Task.Delay(1000, _tokenSource.Token).ContinueWith(Repeated, _tokenSource.Token);
Logger.Info("Starting Reoccuring task");
_tokenSource = new CancellationTokenSource();
_tokenSource.Token.ThrowIfCancellationRequested();
Task.Run(async () =>
{
while (!_tokenSource.IsCancellationRequested)
{
Logger.Debug("Attempting to download and insert player statistics");
CncGeneralInfo info = await GatherData();
InsertData(info);
Thread.Sleep(1000 * 60);
}
}, _tokenSource.Token);
}

public void StopReoccuringTask()
{
Logger.Info("Stopping Reoccuring task for shutdown");
_tokenSource.Cancel();
_tokenSource.Dispose();
}

private async Task<CncGeneralInfo> GatherData()
Expand All @@ -73,6 +82,12 @@ private async Task<CncGeneralInfo> GatherData()
var res = await req.GetResponseAsync();
HttpWebResponse response = (HttpWebResponse) res;

if (response.StatusCode != HttpStatusCode.OK)
{
Logger.Error($"ER.. Could not get a proper status. Statuscode was ${response.StatusCode}");
return info;
}

using (Stream streamResponse = response.GetResponseStream())
using (StreamReader sr =
new StreamReader(streamResponse ?? throw new InvalidOperationException("streamResponse was null")))
Expand Down

0 comments on commit 3c6d4e2

Please sign in to comment.