Skip to content

Commit 99d4069

Browse files
committed
Added example with Task Completion Source
1 parent 46d6ab1 commit 99d4069

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

DiscordRPC.Example/Program.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Text;
44
using System.Threading;
5+
using System.Threading.Tasks;
56

67
namespace DiscordRPC.Example
78
{
@@ -65,7 +66,8 @@ static void Main(string[] args)
6566
}
6667

6768
//Seting a random details to test the update rate of the presence
68-
BasicExample();
69+
//BasicExample();
70+
ReadyTaskExample();
6971
//FullClientExample();
7072
//Issue104();
7173
//IssueMultipleSets();
@@ -235,6 +237,36 @@ static void MainLoop()
235237
Console.ReadKey();
236238
}
237239

240+
static async void ReadyTaskExample()
241+
{
242+
TaskCompletionSource<User> readyCompletionSource = new TaskCompletionSource<User>();
243+
244+
// == Create the client
245+
// We are using the `using` keyword because we want to automatically call Dispose
246+
// once we finish this method. We only care to get the user info.
247+
// If you want to update the presence, dont do this but rather make it a singleton
248+
// that lives throughout the lifetime of your app.
249+
using var client = new DiscordRpcClient("424087019149328395", pipe: discordPipe)
250+
{
251+
Logger = new Logging.ConsoleLogger(logLevel, true)
252+
};
253+
254+
// == Sub to ready
255+
// We are going to listen to the On Ready. Once we have it, we will tell the completion
256+
// source to continue with the result.
257+
client.OnReady += (sender, msg) =>
258+
{
259+
readyCompletionSource.SetResult(msg.User);
260+
};
261+
262+
// == Initialize
263+
client.Initialize();
264+
265+
// == Wait for user
266+
var user = await readyCompletionSource.Task;
267+
Console.WriteLine("Connected to discord with user {0}: {1}", user.Username, user.Avatar);
268+
}
269+
238270
#region Events
239271

240272
#region State Events

0 commit comments

Comments
 (0)