JKorf / Binance.Net

A C# .netstandard client library for the Binance REST and Websocket Spot and Futures API focusing on clear usage and models
https://jkorf.github.io/Binance.Net/
MIT License
1.02k stars 421 forks source link

Cant stream bitcoin price asynchronous #802

Closed naserkaka closed 2 years ago

naserkaka commented 3 years ago

Hi, I have this async test method that stream bitcoin prices. It runs one time and then stops The private BinanceSocketClient socketclient; is declared outside the the method.

M streamPrice()

The result is here , it gets the ticker price once and then stops. But if I have Console.ReadLine(); at line 95 then everything works fine, I stream data non stop.

20210714 235604025 l Binance Debug I Client configuration LogVerbosity Debug, Writers Credentials Set, BaseAddress wss

I call test method from

Program class public static async Task Main() { BinanceNet binance = new BinanceNet(); await binance.test(); }

How can I stream price non-stop without Console.ReadLine(); ?

JKorf commented 3 years ago

Your program stops because your main thread is finished executing. The await binance.test() call only waits for the subscribing to finish, but continuous after that, which means the program is completed at that point. You'll need a mechanism which stops the main thread from exiting. One options is to add a Console.ReadLine(); at the end. You could extend that by checking the input of Console.ReadLine to see if the user really want to exit, something like this:

while(true)
{
  var userInput = Console.ReadLine();
  if(userInput == "q")
    break;
}

This will keep your program running until the user write the letter q in the console.

Or you could do something else, either way, make sure your main thread doesn't exit.

naserkaka commented 3 years ago

Thanks @JKorf !