public void InitDB() { var cfg = Configuration.GetSection("ConnectionString"); int retryCount = 0; while (true) { try { DBCommands.CreateDataBase(cfg["server"], cfg["port"], cfg["user"], cfg["password"], cfg["database"].ToLower()); break; } catch { Console.WriteLine($"Program can't connect to database. Attempt {++retryCount}/5"); if (retryCount >= 5) { Console.WriteLine("All attempts failed. Programm will be closed"); Environment.Exit(0); } Thread.Sleep(3000); } } }