socket.io-client-csharp icon indicating copy to clipboard operation
socket.io-client-csharp copied to clipboard

App terminates with no exceptions after running await client.ConnectAsync()

Open liorsbg opened this issue 2 years ago • 3 comments

On the server side I see a connection and then disconnection immediately after. Looks like we're not really awaiting...

Happened here as well: #291 #287

liorsbg avatar Aug 14 '22 19:08 liorsbg

sorry, I can't get the point, could you explain more?

doghappy avatar Feb 03 '23 14:02 doghappy

client.ConnectAsync() is non-blocking no matter how you call it so unless you have something keeping your app running, it will terminate. I fixed this by just running a thread in the foreground. I create the thread after initializing the rest of my app and connecting the socket.

Thread keepAliveThread = new Thread(() =>
{
    Thread.CurrentThread.IsBackground = false;
    while (true)
    {
        Thread.Sleep(1000);
    }
});

keepAliveThread.Start();

This is only necessary for me because my program will run in the context of a background process with no user interaction and will not have a terminal. If you have a console, you can do something like Console.ReadLine() to resolve the problem as stated in your reference issues.

larryr1 avatar Jul 29 '23 08:07 larryr1

Уou can still do this in async Main method:

await Task.Delay(Timeout.Infinite);

shad0w-jo4n avatar Aug 14 '23 23:08 shad0w-jo4n