1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 06:52:55 +08:00

Merge pull request #2293 from peppy/fix-api-abort

Fix API never stopping its thread
This commit is contained in:
Dean Herbert 2018-03-24 03:34:01 +09:00 committed by GitHub
commit 802ef3e3ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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,8 +45,7 @@ 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 CancellationTokenSource cancellationToken = new CancellationTokenSource();
private readonly Logger log;
@ -59,8 +59,7 @@ namespace osu.Game.Online.API
ProvidedUsername = config.Get<string>(OsuSetting.Username);
Token = config.Get<string>(OsuSetting.Token);
thread = new Thread(run) { IsBackground = true };
thread.Start();
Task.Factory.StartNew(run, cancellationToken.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
}
private readonly List<IOnlineComponent> components = new List<IOnlineComponent>();
@ -93,7 +92,7 @@ namespace osu.Game.Online.API
private void run()
{
while (thread.IsAlive)
while (!cancellationToken.IsCancellationRequested)
{
switch (State)
{
@ -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<bool>(OsuSetting.SavePassword) ? Token : string.Empty);
config.Save();
flushQueue();
cancellationToken.Cancel();
}
}