public void ExecuteCompactDb(ICommandContext context) { ProgressWorker.Run( context, false, progressVisualizer => { progressVisualizer.SetProgressText(Resources.CompactDbProgressText); var janusDatabaseManager = context.GetRequiredService <IJanusDatabaseManager>(); using (janusDatabaseManager.GetLock().GetWriterLock()) using (var con = new SQLiteConnection(janusDatabaseManager.GetCurrentConnectionString())) using (var cmd = con.CreateCommand()) { con.Open(); // This is a REALLY long operation cmd.CommandTimeout = Int32.MaxValue; // Clean up the backend database file cmd.CommandText = @"pragma page_size=" + SqliteSchemaDriver.PageSize + @"; VACUUM; ANALYZE;"; cmd.ExecuteNonQuery(); } }); }
public void ExecuteCompactDb(ICommandContext context) { ProgressWorker.Run( context, false, progressVisualizer => { progressVisualizer.SetProgressText(Resources.CompactDbProgressText); var janusDatabaseManager = context.GetRequiredService <IJanusDatabaseManager>(); using (janusDatabaseManager.GetLock().GetWriterLock()) { var connectionString = janusDatabaseManager.GetCurrentConnectionString(); var csb = new JetConnectionStringBuilder(connectionString); var dbFile = csb.DataSource; var tmpFile = dbFile + ".temp"; var backupFile = dbFile + ".bak"; csb.DataSource = tmpFile; if (File.Exists(tmpFile)) { File.Delete(tmpFile); } OleDbConnection.ReleaseObjectPool(); var engine = (IJetEngine) new JetEngineClass(); try { engine.CompactDatabase(connectionString, csb.ConnectionString); } finally { Marshal.ReleaseComObject(engine); } if (Environment.OSVersion.Platform == PlatformID.Win32Windows) { // Unsafe replace // File.Move(tmpFile, dbFile); File.Delete(tmpFile); } else { // Safe replace // File.Replace(tmpFile, dbFile, backupFile); File.Delete(backupFile); } } }); }
public void ExecuteCompactDb(ICommandContext context) { ProgressWorker.Run( context, false, progressVisualizer => { progressVisualizer.SetProgressText(Resources.CompactDbProgressText); var janusDatabaseManager = context.GetRequiredService <IJanusDatabaseManager>(); using (janusDatabaseManager.GetLock().GetWriterLock()) { var connectionString = janusDatabaseManager.GetCurrentConnectionString(); // Ensure there is no cached connections to the DB // SqlConnection.ClearAllPools(); string dbName; using (var con = MssqlSchemaDriver.ConnectToMaster(connectionString, out dbName)) using (var cmd = con.CreateCommand()) { con.Open(); // This is a REALLY long operation // cmd.CommandTimeout = Int32.MaxValue; // Set the database in single user mode. // cmd.CommandText = @"EXEC sp_dboption '" + dbName + @"', 'single user', 'True'"; cmd.ExecuteNonQuery(); // Shrink the database size to the size + 10 percent of free space. // The truncateonly attribute releases the shrunken space to // the operating system. // cmd.CommandText = @"DBCC ShrinkDatabase(" + dbName + @", 10, TRUNCATEONLY)"; cmd.ExecuteNonQuery(); // Checks the integrity of the db, and repairs some issues without // data loss. This will rebuild your indexes. // cmd.CommandText = @"DBCC CheckDB(" + dbName + @", REPAIR_REBUILD)"; cmd.ExecuteNonQuery(); // Sets the db back to full access. // cmd.CommandText = @"EXEC sp_dboption '" + dbName + @"', 'single user', 'False'"; cmd.ExecuteNonQuery(); } } }); }
void Run() { if (_running) { MessageBox.Show( "The neural network is training. Wait until done.", "Already running", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } ProgressWorkerParams workerParams = new ProgressWorkerParams { Alignment = System.Drawing.ContentAlignment.BottomLeft, AllowCancel = true, ControlAnchor = frmMain.GInstance.MActiveChart, Modal = true, }; ProgressWorker.Run( workerParams, visualizer => { try { visualizer.SetProgressTitle("Neural Network"); visualizer.SetProgressAction("Initializing..."); AsyncNeuralNetwork worker = new AsyncNeuralNetwork(); bool first = true; worker.ProgressCallback += (object o, int records, int record, ref bool stop) => { if (first) { first = false; visualizer.InitProgress(0, records); } visualizer.ReportProgress(record); stop = visualizer.CancelReqested; }; visualizer.SetProgressAction("Training Neural Network..."); List <string> items = new List <string>(); foreach (NListBoxItem item in lstSelected.Items) { items.Add(item.Text); } // Do the work worker.DoWork(items); // Exit if no report if (worker.ResultCount < 1 || visualizer.CancelReqested) { return; // No work to report } visualizer.SetProgressAction("Completing..."); visualizer.InitProgress(0, worker.ResultCount); for (int n = 0; n < worker.ResultCount; ++n) { visualizer.ReportProgress(n); if (visualizer.CancelReqested) { break; } } } catch (Exception ex) { _asyncOp.Post( () => MessageBox.Show( "An error occured. Error :" + Environment.NewLine + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)); } finally { _running = false; } }, () => { }); }
void UseThisFunctionToDoTheAsyncWork() { if (_running) { MessageBox.Show( "The work is in progress. Wait until done.", "Already running", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } ProgressWorkerParams workerParams = new ProgressWorkerParams { Alignment = System.Drawing.ContentAlignment.BottomLeft, AllowCancel = true, ControlAnchor = this, Modal = true, }; ProgressWorker.Run( workerParams, visualizer => { try { visualizer.SetProgressTitle("Feature name goes here"); visualizer.SetProgressAction("Working..."); YourWorkerClass worker = new YourWorkerClass(); bool first = true; worker.ProgressCallback += (object o, int records, int record, ref bool stop) => { if (first) { first = false; visualizer.InitProgress(0, records); } visualizer.ReportProgress(record); stop = visualizer.CancelReqested; }; visualizer.SetProgressAction("Initializing..."); // Do the work worker.DoWork(); // Exit if no report if (worker.ResultCount < 1 || visualizer.CancelReqested) return; // No work to report visualizer.SetProgressAction("Completing..."); visualizer.InitProgress(0, worker.ResultCount); for(int n = 0; n < worker.ResultCount; ++n) { visualizer.ReportProgress(n); if (visualizer.CancelReqested) break; } } catch (Exception ex) { _asyncOp.Post( () => MessageBox.Show( "An error occured. Error :" + Environment.NewLine + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)); } finally { _running = false; } }, () => { }); }