Promise completions should be executed on the thread pool

This commit is contained in:
RogueException
2016-07-14 14:23:23 -03:00
parent 99703d2f16
commit 1aa908512f
5 changed files with 32 additions and 9 deletions

View File

@@ -230,7 +230,7 @@ namespace Discord.Audio
_secretKey = data.SecretKey;
await ApiClient.SendSetSpeaking(true).ConfigureAwait(false);
_connectTask.TrySetResult(true);
var _ = _connectTask.TrySetResultAsync(true);
}
break;
case VoiceOpCode.HeartbeatAck:

View File

@@ -530,8 +530,8 @@ namespace Discord
await _readyEvent.InvokeAsync().ConfigureAwait(false);
await SyncGuildsAsync().ConfigureAwait(false);
_connectTask.TrySetResult(true); //Signal the .Connect() call to complete
var _ = Task.Run(() => _connectTask.TrySetResult(true)); //Signal the .Connect() call to complete
await _gatewayLogger.InfoAsync("Ready").ConfigureAwait(false);
}
break;

View File

@@ -96,9 +96,9 @@ namespace Discord
AddUser(model.Members[i], dataStore, members);
if (Discord.ApiClient.AuthTokenType != TokenType.User)
{
_syncPromise.TrySetResult(true);
var _ = _syncPromise.TrySetResultAsync(true);
if (!model.Large)
_downloaderPromise.TrySetResult(true);
_ = _downloaderPromise.TrySetResultAsync(true);
}
for (int i = 0; i < model.Presences.Length; i++)
@@ -122,9 +122,9 @@ namespace Discord
DownloadedMemberCount = 0;
for (int i = 0; i < model.Members.Length; i++)
AddUser(model.Members[i], dataStore, members);
_syncPromise.TrySetResult(true);
var _ = _syncPromise.TrySetResultAsync(true);
if (!model.Large)
_downloaderPromise.TrySetResult(true);
_ = _downloaderPromise.TrySetResultAsync(true);
for (int i = 0; i < model.Presences.Length; i++)
AddOrUpdateUser(model.Presences[i], dataStore, members);
@@ -233,7 +233,7 @@ namespace Discord
}
public void CompleteDownloadMembers()
{
_downloaderPromise.TrySetResult(true);
_downloaderPromise.TrySetResultAsync(true);
}
public VoiceState AddOrUpdateVoiceState(VoiceStateModel model, DataStore dataStore, ConcurrentDictionary<ulong, VoiceState> voiceStates = null)

View File

@@ -0,0 +1,23 @@
using System;
using System.Threading.Tasks;
namespace Discord
{
internal static class TaskCompletionSourceExtensions
{
public static Task SetResultAsync<T>(this TaskCompletionSource<T> source, T result)
=> Task.Run(() => source.SetResult(result));
public static Task<bool> TrySetResultAsync<T>(this TaskCompletionSource<T> source, T result)
=> Task.Run(() => source.TrySetResult(result));
public static Task SetExceptionAsync<T>(this TaskCompletionSource<T> source, Exception ex)
=> Task.Run(() => source.SetException(ex));
public static Task<bool> TrySetExceptionAsync<T>(this TaskCompletionSource<T> source, Exception ex)
=> Task.Run(() => source.TrySetException(ex));
public static Task SetCanceledAsync<T>(this TaskCompletionSource<T> source)
=> Task.Run(() => source.SetCanceled());
public static Task<bool> TrySetCanceledAsync<T>(this TaskCompletionSource<T> source)
=> Task.Run(() => source.TrySetCanceled());
}
}

View File

@@ -141,7 +141,7 @@ namespace Discord.Net.Queue
private async Task QueueResumeAsync(TaskCompletionSource<byte> resumeNotifier, int millis)
{
await Task.Delay(millis).ConfigureAwait(false);
resumeNotifier.SetResult(0);
resumeNotifier.TrySetResultAsync<byte>(0);
}
private async Task EnterAsync(int? endTick)