more IAsyncDisposable stuff

This commit is contained in:
Wizou 2024-09-07 01:59:27 +02:00
parent 9fe6a9d74f
commit be7027b318
6 changed files with 10 additions and 7 deletions

View file

@ -11,7 +11,7 @@ namespace WTelegramClientTest
static async Task Main(string[] _) static async Task Main(string[] _)
{ {
Console.WriteLine("The program will download photos/medias from messages you send/forward to yourself (Saved Messages)"); Console.WriteLine("The program will download photos/medias from messages you send/forward to yourself (Saved Messages)");
using var client = new WTelegram.Client(Environment.GetEnvironmentVariable); await using var client = new WTelegram.Client(Environment.GetEnvironmentVariable);
var user = await client.LoginUserIfNeeded(); var user = await client.LoginUserIfNeeded();
client.OnUpdates += Client_OnUpdates; client.OnUpdates += Client_OnUpdates;
Console.ReadKey(); Console.ReadKey();

View file

@ -24,7 +24,7 @@ namespace WTelegramClientTest
static async Task Main(string[] _) static async Task Main(string[] _)
{ {
using var client = new WTelegram.Client(Config); await using var client = new WTelegram.Client(Config);
var user = await client.LoginUserIfNeeded(); var user = await client.LoginUserIfNeeded();
Console.WriteLine($"We are logged-in as {user.username ?? user.first_name + " " + user.last_name} (id {user.id})"); Console.WriteLine($"We are logged-in as {user.username ?? user.first_name + " " + user.last_name} (id {user.id})");

View file

@ -28,7 +28,7 @@ namespace WTelegramClientTest
var store = new PostgreStore(Environment.GetEnvironmentVariable("DATABASE_URL"), Environment.GetEnvironmentVariable("SESSION_NAME")); var store = new PostgreStore(Environment.GetEnvironmentVariable("DATABASE_URL"), Environment.GetEnvironmentVariable("SESSION_NAME"));
// if DB does not contain a session yet, client will be run in interactive mode // if DB does not contain a session yet, client will be run in interactive mode
Client = new WTelegram.Client(store.Length == 0 ? null : Environment.GetEnvironmentVariable, store); Client = new WTelegram.Client(store.Length == 0 ? null : Environment.GetEnvironmentVariable, store);
using (Client) await using (Client)
{ {
Client.OnUpdates += Client_OnUpdates; Client.OnUpdates += Client_OnUpdates;
My = await Client.LoginUserIfNeeded(); My = await Client.LoginUserIfNeeded();

View file

@ -16,7 +16,7 @@ namespace WTelegramClientTest
Console.WriteLine("The program will display updates received for the logged-in user. Press any key to terminate"); Console.WriteLine("The program will display updates received for the logged-in user. Press any key to terminate");
WTelegram.Helpers.Log = (l, s) => System.Diagnostics.Debug.WriteLine(s); WTelegram.Helpers.Log = (l, s) => System.Diagnostics.Debug.WriteLine(s);
Client = new WTelegram.Client(Environment.GetEnvironmentVariable); Client = new WTelegram.Client(Environment.GetEnvironmentVariable);
using (Client) await using (Client)
{ {
Manager = Client.WithUpdateManager(Client_OnUpdate/*, "Updates.state"*/); Manager = Client.WithUpdateManager(Client_OnUpdate/*, "Updates.state"*/);
My = await Client.LoginUserIfNeeded(); My = await Client.LoginUserIfNeeded();

View file

@ -20,7 +20,7 @@ namespace WTelegramClientTest
} }
finally finally
{ {
Client?.Dispose(); if (Client != null) await Client.DisposeAsync();
} }
} }
@ -42,7 +42,7 @@ namespace WTelegramClientTest
while (true) while (true)
{ {
Console.WriteLine("Disposing the client and trying to reconnect in 5 seconds..."); Console.WriteLine("Disposing the client and trying to reconnect in 5 seconds...");
Client?.Dispose(); if (Client != null) await Client.DisposeAsync();
Client = null; Client = null;
await Task.Delay(5000); await Task.Delay(5000);
try try

View file

@ -20,7 +20,10 @@ using static WTelegram.Encryption;
namespace WTelegram namespace WTelegram
{ {
public partial class Client : IDisposable, IAsyncDisposable public partial class Client : IDisposable
#if NETCOREAPP2_1_OR_GREATER
, IAsyncDisposable
#endif
{ {
/// <summary>This event will be called when unsollicited updates/messages are sent by Telegram servers</summary> /// <summary>This event will be called when unsollicited updates/messages are sent by Telegram servers</summary>
/// <remarks>Make your handler <see langword="async"/>, or return <see cref="Task.CompletedTask"/> or <see langword="null"/><br/>See <see href="https://github.com/wiz0u/WTelegramClient/blob/master/Examples/Program_ReactorError.cs?ts=4#L30">Examples/Program_ReactorError.cs</see> for how to use this<br/>or <see href="https://github.com/wiz0u/WTelegramClient/blob/master/Examples/Program_ListenUpdates.cs?ts=4#L21">Examples/Program_ListenUpdate.cs</see> using the UpdateManager class instead</remarks> /// <remarks>Make your handler <see langword="async"/>, or return <see cref="Task.CompletedTask"/> or <see langword="null"/><br/>See <see href="https://github.com/wiz0u/WTelegramClient/blob/master/Examples/Program_ReactorError.cs?ts=4#L30">Examples/Program_ReactorError.cs</see> for how to use this<br/>or <see href="https://github.com/wiz0u/WTelegramClient/blob/master/Examples/Program_ListenUpdates.cs?ts=4#L21">Examples/Program_ListenUpdate.cs</see> using the UpdateManager class instead</remarks>