mirror of
https://github.com/ppy/osu.git
synced 2025-01-07 21:32:57 +08:00
Merge pull request #2293 from peppy/fix-api-abort
Fix API never stopping its thread
This commit is contained in:
commit
802ef3e3ff
@ -7,6 +7,7 @@ using System.Collections.Generic;
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using osu.Framework.Configuration;
|
using osu.Framework.Configuration;
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
using osu.Framework.Logging;
|
using osu.Framework.Logging;
|
||||||
@ -44,8 +45,7 @@ namespace osu.Game.Online.API
|
|||||||
|
|
||||||
protected bool HasLogin => Token != null || !string.IsNullOrEmpty(ProvidedUsername) && !string.IsNullOrEmpty(password);
|
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 CancellationTokenSource cancellationToken = new CancellationTokenSource();
|
||||||
private readonly Thread thread;
|
|
||||||
|
|
||||||
private readonly Logger log;
|
private readonly Logger log;
|
||||||
|
|
||||||
@ -59,8 +59,7 @@ namespace osu.Game.Online.API
|
|||||||
ProvidedUsername = config.Get<string>(OsuSetting.Username);
|
ProvidedUsername = config.Get<string>(OsuSetting.Username);
|
||||||
Token = config.Get<string>(OsuSetting.Token);
|
Token = config.Get<string>(OsuSetting.Token);
|
||||||
|
|
||||||
thread = new Thread(run) { IsBackground = true };
|
Task.Factory.StartNew(run, cancellationToken.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
|
||||||
thread.Start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly List<IOnlineComponent> components = new List<IOnlineComponent>();
|
private readonly List<IOnlineComponent> components = new List<IOnlineComponent>();
|
||||||
@ -93,7 +92,7 @@ namespace osu.Game.Online.API
|
|||||||
|
|
||||||
private void run()
|
private void run()
|
||||||
{
|
{
|
||||||
while (thread.IsAlive)
|
while (!cancellationToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
switch (State)
|
switch (State)
|
||||||
{
|
{
|
||||||
@ -267,10 +266,7 @@ namespace osu.Game.Online.API
|
|||||||
|
|
||||||
public bool IsLoggedIn => LocalUser.Value.Id > 1;
|
public bool IsLoggedIn => LocalUser.Value.Id > 1;
|
||||||
|
|
||||||
public void Queue(APIRequest request)
|
public void Queue(APIRequest request) => queue.Enqueue(request);
|
||||||
{
|
|
||||||
queue.Enqueue(request);
|
|
||||||
}
|
|
||||||
|
|
||||||
public event StateChangeDelegate OnStateChange;
|
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.Set(OsuSetting.Token, config.Get<bool>(OsuSetting.SavePassword) ? Token : string.Empty);
|
||||||
config.Save();
|
config.Save();
|
||||||
|
|
||||||
|
flushQueue();
|
||||||
|
cancellationToken.Cancel();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user