|
2 | 2 | using System; |
3 | 3 | using System.Text; |
4 | 4 | using System.Threading; |
| 5 | +using System.Threading.Tasks; |
5 | 6 |
|
6 | 7 | namespace DiscordRPC.Example |
7 | 8 | { |
@@ -65,7 +66,8 @@ static void Main(string[] args) |
65 | 66 | } |
66 | 67 |
|
67 | 68 | //Seting a random details to test the update rate of the presence |
68 | | - BasicExample(); |
| 69 | + //BasicExample(); |
| 70 | + ReadyTaskExample(); |
69 | 71 | //FullClientExample(); |
70 | 72 | //Issue104(); |
71 | 73 | //IssueMultipleSets(); |
@@ -235,6 +237,36 @@ static void MainLoop() |
235 | 237 | Console.ReadKey(); |
236 | 238 | } |
237 | 239 |
|
| 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 | + |
238 | 270 | #region Events |
239 | 271 |
|
240 | 272 | #region State Events |
|
0 commit comments