Business.DatabaseHandler.EnsureAvailableAsync C# (CSharp) Метод

EnsureAvailableAsync() публичный Метод

Checks server and database availability. If there is no database file, copy and attach initial database. Update database if it is outdated.
If connection fails, the exception must be handled by the caller.
public EnsureAvailableAsync ( ) : Task
Результат Task
        public async Task EnsureAvailableAsync() {
            Version CurrentVersion = null;
            try {
                // Test query.
                CurrentVersion = await Task.Run(() => VersionAccess.GetVersionInfo());
            } catch {
            }

            if (CurrentVersion == null) {
                // Check if database exists. If it doesn't, create blank database.
                if (!File.Exists(Settings.DatabasePath) || new FileInfo(Settings.DatabasePath).Length == 0)
                    await CreateNewDatabaseAsync();

                await TryUntilTimeout(() => VersionAccess.GetVersionInfo(), 10000);
            }

            // If database connection is successfull, ensure database file is up to date.
            await UpdateDatabaseAsync();
        }

Usage Example

 private async Task TestDatabaseConnectionAsync() {
     // Test connection to database.
     DatabaseHandler db = new DatabaseHandler();
     try {
         await db.EnsureAvailableAsync();
     }
     catch (Exception ex) {
         string ErrorMessage = "Cannot connect to database.\r\n" + ex.Message;
         MessageBox.Show(Main, ErrorMessage, "Natural Grounding Player");
         App.Current.Shutdown();
     }
 }