Guest User

TryOpenConnectionAsync Code

a guest
Sep 29th, 2025
25
0
339 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.47 KB | None | 0 0
  1. public async Task<bool> TryOpenConnectionAsync(MySqlConnection connection, int timeoutSeconds = 20, bool hasLock = false)
  2. {
  3.     if (!hasLock) await _syncLock.WaitAsync();
  4.     try
  5.     {
  6.         if (connection.State == ConnectionState.Open)
  7.         {
  8.             // Sometimes the database gets disconnected so the program thinks the connection is active and throws an exception when trying to use it. By pinging, I make sure it's actually still connected.
  9.             if (!await connection.PingAsync())
  10.             {
  11.                 await connection.CloseAsync();
  12.                 return false;
  13.             }
  14.             else return true;
  15.         }
  16.  
  17.         if (connection.State != ConnectionState.Closed || connection.State == ConnectionState.Connecting)
  18.         {
  19.             // Can't open if it's Connecting, Executing, etc.
  20.             return false;
  21.         }
  22.  
  23.         try
  24.         {
  25.             var openTask = Task.Run(async () =>
  26.             {
  27.                 try
  28.                 {
  29.                     await connection.OpenAsync();
  30.                     return true;
  31.                 }
  32.                 catch (Exception ex)
  33.                 {
  34.                     if (ex is MySqlException mysqlEx && mysqlEx.Number == 1042)
  35.                     {
  36.                         return false;
  37.                     }
  38.                     LogError(ex);
  39.                     return false;
  40.                 }
  41.             });
  42.  
  43.             if (await Task.WhenAny(openTask, Task.Delay(TimeSpan.FromSeconds(timeoutSeconds))) == openTask)
  44.             {
  45.                 return openTask.Result;
  46.             }
  47.             else
  48.             {
  49.                 await connection.CloseAsync(); // Clean up
  50.                 return false;
  51.             }
  52.         }
  53.         catch
  54.         {
  55.             await connection.CloseAsync();
  56.             return false;
  57.         }
  58.     }
  59.     finally
  60.     {
  61.         if (!hasLock) _syncLock.Release();
  62.     }
  63. }
  64.  
  65. // Sync loop:
  66. Task.Run(async () =>
  67. {
  68.     await Task.Delay(TimeSpan.FromSeconds(5));
  69.     while (true)
  70.     {
  71.         await _syncLock.WaitAsync();
  72.         try
  73.         {
  74.             await TryUploadTransactionsAsync();
  75.         }
  76.         catch (Exception ex)
  77.         {
  78.             if (ex is not MySqlException mysqlEx || mysqlEx.Number != 1042)
  79.                 LogError(ex);
  80.             ErrorMessageBox(ex);
  81.         }
  82.         finally
  83.         {
  84.             _syncLock.Release();
  85.         }
  86.         await Task.Delay(TimeSpan.FromSeconds(10));
  87.     }
  88. });
Advertisement
Add Comment
Please, Sign In to add comment