private void LoadDataTableAsync(DiscoveredServer server, string sql) { //it is already running and not completed if (_task != null && !_task.IsCompleted) { return; } HideFatal(); pbLoading.Visible = true; llCancel.Visible = true; _task = Task.Factory.StartNew(() => { int timeout = 1000; while (!IsHandleCreated && timeout > 0) { timeout -= 10; Thread.Sleep(10); } try { //then execute the command using (DbConnection con = server.GetConnection()) { con.Open(); _cmd = server.GetCommand(sql, con); _cmd.CommandTimeout = _timeoutControls.Timeout; DbDataAdapter a = server.GetDataAdapter(_cmd); DataTable dt = new DataTable(); a.Fill(dt); MorphBinaryColumns(dt); Invoke(new MethodInvoker(() => { dataGridView1.DataSource = dt; })); con.Close(); } } catch (Exception e) { ShowFatal(e); } finally { if (IsHandleCreated) { Invoke(new MethodInvoker(() => { pbLoading.Visible = false; llCancel.Visible = false; })); } } }); }
private void FillTableWithQueryIfUserConsents(DataTable dt, string sql, ICheckNotifier checkNotifier, DiscoveredServer server) { bool execute = checkNotifier.OnCheckPerformed(new CheckEventArgs("About to fetch data, confirming user is happy with SQL", CheckResult.Warning, null, sql)); if (execute) { using (var con = server.GetConnection()) { con.Open(); using (var cmd = server.GetCommand(sql, con)) { cmd.CommandTimeout = _timeout; var da = server.GetDataAdapter(cmd); da.Fill(dt); } } } else { checkNotifier.OnCheckPerformed(new CheckEventArgs("User decided not to execute the SQL", CheckResult.Fail)); } }