From 197089b1e0eb6ca8bc270ae9222b4c14703bcb2e Mon Sep 17 00:00:00 2001 From: Brandon Smith Date: Tue, 29 Sep 2015 17:09:06 -0300 Subject: [PATCH] Fixed outgoing-only sockets. Minor performance patch. --- .../WebSockets/Voice/VoiceWebSocket.cs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Discord.Net/WebSockets/Voice/VoiceWebSocket.cs b/src/Discord.Net/WebSockets/Voice/VoiceWebSocket.cs index 332f6afd9..6a13660e1 100644 --- a/src/Discord.Net/WebSockets/Voice/VoiceWebSocket.cs +++ b/src/Discord.Net/WebSockets/Voice/VoiceWebSocket.cs @@ -132,22 +132,35 @@ namespace Discord.WebSockets.Voice tasks.Add(SendVoiceAsync()); #endif } + + //This thread is required to establish a connection even if we're outgoing only +#if USE_THREAD if ((_client.Config.VoiceMode & DiscordVoiceMode.Incoming) != 0) { -#if USE_THREAD _receiveThread = new Thread(new ThreadStart(() => ReceiveVoiceAsync(_cancelToken))); _receiveThread.Start(); + } + else //Dont make an OS thread if we only want to capture one packet... + tasks.Add(Task.Run(() => ReceiveVoiceAsync(_cancelToken))); #else tasks.Add(ReceiveVoiceAsync()); #endif - } #if !DNXCORE50 tasks.Add(WatcherAsync()); #endif + if (tasks.Count > 0) + { + // We need to combine tasks into one because receiveThread is + // supposed to exit early if it's an outgoing-only client + // and we dont want the main thread to think we errored + var task = Task.WhenAll(tasks); + tasks.Clear(); + tasks.Add(task); + } tasks.AddRange(base.GetTasks()); - return tasks.ToArray(); + return new Task[] { Task.WhenAll(tasks.ToArray()) }; } protected override Task Cleanup() {