private async System.Threading.Tasks.Task <bool> CreateConnectionAsync() { bool ret = false; try { Stopwatch sw = Stopwatch.StartNew(); this.HistoryDic.Clear(); this.HasChanged.WriteFullFence(false); this.DeletedCount.WriteFullFence(0); this.AddedCount.WriteFullFence(0); //FullMutex for safe multithreading SQLiteOpenFlags flags = SQLiteOpenFlags.Create | SQLiteOpenFlags.FullMutex; if (this.ReadOnly) { flags = flags | SQLiteOpenFlags.ReadOnly; } else { flags = flags | SQLiteOpenFlags.ReadWrite; } if (this.IsSQLiteDBConnected()) { this.DisposeConnection(); } //If the database file doesn't exist, the default behaviour is to create a new file sqlite_conn = new SQLiteAsyncConnection(this.Filename, flags, true); //make sure table exists: CreateTableResult ctr = await sqlite_conn.CreateTableAsync <History>(); await sqlite_conn.ExecuteScalarAsync <int>(@"PRAGMA journal_mode = 'WAL';", new object[] { }); await sqlite_conn.ExecuteScalarAsync <int>(@"PRAGMA busy_timeout = 30000;", new object[] { }); sw.Stop(); Global.Log($"Created connection to SQLite db v{sqlite_conn.LibVersionNumber} in {sw.ElapsedMilliseconds}ms - TableCreate={ctr.ToString()}: {this.Filename}"); HasChanged.WriteFullFence(true); } catch (Exception ex) { Global.Log("Error: " + Global.ExMsg(ex)); } return(ret); }
public async Task <bool> MigrateHistoryCSV(string Filename) { bool ret = false; try { if (!this.IsSQLiteDBConnected()) { await this.CreateConnectionAsync(); } //run in another thread so we dont block UI await Task.Run(async() => { if (System.IO.File.Exists(Filename)) { Global.Log("Migrating history list from cameras/history.csv ..."); Stopwatch SW = Stopwatch.StartNew(); //delete obsolete entries from history.csv //CleanCSVList(); //removed to load the history list faster List <string> result = new List <string>(); //List that later on will be containing all lines of the csv file bool Success = await Global.WaitForFileAccessAsync(Filename); if (Success) { //load all lines except the first line into List (the first line is the table heading and not an alert entry) foreach (string line in System.IO.File.ReadAllLines(Filename).Skip(1)) { result.Add(line); } Global.Log($"...Found {result.Count} lines."); List <string> itemsToDelete = new List <string>(); //stores all filenames of history.csv entries that need to be removed //load all List elements into the ListView for each row int added = 0; foreach (var val in result) { History hist = new History().CreateFromCSV(val); if (File.Exists(hist.Filename)) { if (await this.InsertHistoryItem(hist)) { added++; //this.AddedCount.AtomicIncrementAndGet(); } } } ret = (added > 0); //try to get a better feel how much time this function consumes - Vorlon Global.Log($"...Added {added} out of {result.Count} history items in {{yellow}}{SW.ElapsedMilliseconds}ms{{white}}, {this.HistoryDic.Count()} lines."); } else { Global.Log($"Error: Could not gain access to history file for {SW.ElapsedMilliseconds}ms - {AppSettings.Settings.HistoryFileName}"); } } else { Global.Log($"File does not exist, could not migrate: {Filename}"); } }); } catch (Exception ex) { Global.Log("Error: " + Global.ExMsg(ex)); } if (ret) { HasChanged.WriteFullFence(true); } return(ret); }