From 6c4e719e0f675af3724a45862a9338c4038681e0 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 23 Mar 2018 15:20:19 +0900 Subject: [PATCH 1/2] Fix API never stopping its thread --- osu.Game/Online/API/APIAccess.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/osu.Game/Online/API/APIAccess.cs b/osu.Game/Online/API/APIAccess.cs index 2cb8424bcc..39ecde55bc 100644 --- a/osu.Game/Online/API/APIAccess.cs +++ b/osu.Game/Online/API/APIAccess.cs @@ -44,7 +44,6 @@ namespace osu.Game.Online.API protected bool HasLogin => Token != null || !string.IsNullOrEmpty(ProvidedUsername) && !string.IsNullOrEmpty(password); - // ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable (should dispose of this or at very least keep a reference). private readonly Thread thread; private readonly Logger log; @@ -267,10 +266,7 @@ namespace osu.Game.Online.API public bool IsLoggedIn => LocalUser.Value.Id > 1; - public void Queue(APIRequest request) - { - queue.Enqueue(request); - } + public void Queue(APIRequest request) => queue.Enqueue(request); public event StateChangeDelegate OnStateChange; @@ -312,6 +308,9 @@ namespace osu.Game.Online.API config.Set(OsuSetting.Token, config.Get(OsuSetting.SavePassword) ? Token : string.Empty); config.Save(); + + flushQueue(); + thread?.Abort(); } } From f0c0a5110819716e9bfffcaf7f22a681f5e4e88d Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 23 Mar 2018 20:57:04 +0900 Subject: [PATCH 2/2] Convert APIAccess to use cancellation tokens --- osu.Game/Online/API/APIAccess.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/osu.Game/Online/API/APIAccess.cs b/osu.Game/Online/API/APIAccess.cs index 39ecde55bc..957aeac3cd 100644 --- a/osu.Game/Online/API/APIAccess.cs +++ b/osu.Game/Online/API/APIAccess.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Net; using System.Threading; +using System.Threading.Tasks; using osu.Framework.Configuration; using osu.Framework.Graphics; using osu.Framework.Logging; @@ -44,7 +45,7 @@ namespace osu.Game.Online.API protected bool HasLogin => Token != null || !string.IsNullOrEmpty(ProvidedUsername) && !string.IsNullOrEmpty(password); - private readonly Thread thread; + private readonly CancellationTokenSource cancellationToken = new CancellationTokenSource(); private readonly Logger log; @@ -58,8 +59,7 @@ namespace osu.Game.Online.API ProvidedUsername = config.Get(OsuSetting.Username); Token = config.Get(OsuSetting.Token); - thread = new Thread(run) { IsBackground = true }; - thread.Start(); + Task.Factory.StartNew(run, cancellationToken.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default); } private readonly List components = new List(); @@ -92,7 +92,7 @@ namespace osu.Game.Online.API private void run() { - while (thread.IsAlive) + while (!cancellationToken.IsCancellationRequested) { switch (State) { @@ -310,7 +310,7 @@ namespace osu.Game.Online.API config.Save(); flushQueue(); - thread?.Abort(); + cancellationToken.Cancel(); } }