0

My app works good if there is an internet connection before app started, but if I started my app without any internet connection, the app still retries connecting every 5s and shows in the log: A socket operation was attempted to an unreachable host.

That's ok till now. The problem is when I give my pc internet connection again (and the app is running) it could not re-establish the connection by itself, only if I restarted the app, log shows: The process cannot access the file D:\Documents\VS2022_repos\MySSharp\bin\WTelegram.session because it is being used by another process.

In spite of: dispose and null the client! I assumed that dispose releases the session file, so I can re-create a new client with: new WTelegram.Client(...

Note: I need client after connecting, so it must not be disposed after successfully connected.

This is my code:

public async Task Login_start()
{
    while (true)
    {
        try
        {
            cG.print_inlog($"Connecting & login into Telegram servers...");
            client = new WTelegram.Client(int.Parse(Config_func("api_id")), Config_func("api_hash"));

            break;
        }
        catch (Exception ex)
        {
            cG.print_inlog($"Error in Login_start: {ex.Message}");
            await Task.Delay(3000);
        }
    }
   
    await Login_do(Config_func("phone_number"));
}

public string what;

public async Task<string?> Login_do(string loginInfo)
{
    while (true)
    {
        try
        {
            cG.print_inlog($"connecting...");
            what = await client?.Login(loginInfo);
            break;
        }
        catch (Exception ex)
        {
            cG.print_inlog($"retry connecting in 5s... {ex.Message}");
            client?.Dispose();
            client = null;

            //client?.Reset(false, true); //i tried it also but same problem
            await Task.Delay(5000);

            await Login_start(); // try login again
        }
    }

    if (what is not null) // if what is null means logged in successfully
    {
        cG.print_inlog($"A {what} is required...");
        fm.FrmMain.Text = what + ':';
        fm.TextBoxCode.Text = "";
        fm.labelCode.Visible = true;
        fm.TextBoxCode.Focus();
        return what;
    }
    
    cG.print_inlog($"We are now connected as {client.User}");

    // here i reuse client
    var chs = (await client.Messages_GetAllChats()).chats;
}

This is my log:

Connecting & login into Telegram servers...
Loaded previous session
WTelegramClient 3.5.1 running under .NET 7.0.3
Login_start ok
connecting...
Connecting to 149.154.167.91:443...
SocketException HostUnreachable (10065): A socket operation was attempted to an unreachable host.
Connecting to [2001:67c:4e8:f004::a]:443...
Connecting to 149.154.167.50:443...
Exception thrown: 'System.Net.Sockets.SocketException' in System.Private.CoreLib.dll
retry connecting in 5s... A socket operation was attempted to an unreachable host.

0>Disposing the client
Connecting & login into Telegram servers...
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Error in Login_start: The process cannot access the file 'D:\Documents\VS2022_repos\MySSharp\bin\WTelegram.session' because it is being used by another process.
client = null
Connecting & login into Telegram servers...
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Error in Login_start: The process cannot access the file 'D:\Documents\VS2022_repos\MySSharp\bin\WTelegram.session' because it is being used by another process.
client = null
Connecting & login into Telegram servers...
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Error in Login_start: The process cannot access the file 'D:\Documents\VS2022_repos\MySSharp\bin\WTelegram.session' because it is being used by another process.
client = null
Connecting & login into Telegram servers...
Exception thrown: 'System.IO.IOException' in System.Private.CoreLib.dll
Error in Login_start: The process cannot access the file 'D:\Documents\VS2022_repos\MySSharp\bin\WTelegram.session' because it is being used by another process.
client = null

I tried examples like: Program_ReactorError.cs

tried: @Wizou answer here also: WTelegramClient on Azure Function session file is being used by another process

search in GitHub examples but still the same

Florian
  • 1,019
  • 6
  • 22
  • client.Dispose(); does indeed close the session file. But your code is such a mess... ‍♂️ with methods recursively calling each other ‍♂️ – Wizou Jun 02 '23 at 13:51
  • So now more cleared the code, as i followed your exemples in GitHub, in my case i assumed the user started the app before his pc get connected to internet, i want the app automatically re-establishe the connection. – Tecnosoft-dev Jun 02 '23 at 15:56
  • i still confused Why the file still being used after disposing client – Tecnosoft-dev Jun 04 '23 at 14:28

0 Answers0