如何取消显示的异步oledb查询?

时间:2019-07-15 21:18:43

标签: c# oledb

我正在尝试取消一个异步运行的oledb查询。我尝试了取消令牌方法,但无法使其正常工作。似乎没有任何与取消oledb查询有关的youtube视频或堆栈溢出问题。 sql服务器查询上有很多东西,但oledb没有。


At the Global level:
CancellationTokenSource cts = new CancellationTokenSource();

private async void btnQueryDatabases_Click(object sender, EventArgs e)
        {
            //Disable the Query Button     
            btnQueryDatabases.Enabled = false;
            DateTime startTime = DateTime.Now;
            Waitform wf = new Waitform();
            wf.startUp();
            wf.Visible = true;
            wf.Refresh();

            try
            {
                if (chkIncludeDLabResults.Checked == true)
                {
                    string SQL = DLABSQL();
                    string connectionstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=K:\R&D Dept\Development Lab\DLab Databases and Add Ins\DLab Results Database\Development Lab Results Database.accdb;Jet OLEDB:Database Password='xxxxxxxx'";

                    try
                    {
                        var dataTable = await Task.Run(() => RunDLAsync(SQL, connectionstring, cts.Token));
                    }
                    catch
                    {
                        //MessageBox.Show("This query has been cancelled.", "Query Cancellation");
                    }                    

                    adgvDLabResults.DataSource = myTable;
                }

                wf.aTimer.Enabled = false;

                if (CombinedDT.Rows.Count > 0)
                {
                    adgvDLabResults.DataSource = myTable;
                    adgvCombined.DataSource = myTable;
                }
                else
                {
                    CombinedDT.Merge(myTable, true);
                    adgvCombined.DataSource = myTable;
                }

                int Records = adgvDLabResults.RowCount;
                tsLabel.Text = "Rows Returned = " + Records.ToString();
            }
            catch (OperationCanceledException)
            {
                MessageBox.Show("This query is being cancelled.", "Query Cancellation");
            }           
            finally
            {
                btnQueryDatabases.Enabled = true;

                DateTime endTime = DateTime.Now;
                wf.lblEnd.Text = DateTime.Now.ToString();
                wf.lblElapsed.Text = (endTime - startTime).ToString();

                DialogResult result = MessageBox.Show("Clicking OK Will Exit This Screen and the Query Time Screen Also.", "End of Query Run", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    tpg.SelectedTab = this.tpgALAT;
                    tpg.SelectedTab = this.tpgSearchFor;
                    this.Refresh();
                    wf.Close();
                }
            }
        }

private DataTable RunDLAsync(string commandText, string connectionstring, CancellationToken cancellationToken)
        {
            try
            {            
                using (var connection = new OleDbConnection(connectionstring))
                {                   
                    using (var command = connection.CreateCommand())
                    {                       
                        command.CommandType = CommandType.Text;
                        command.CommandText = commandText;

                        using (var dataAdapter = new OleDbDataAdapter(command))
                        {          
                            connection.Open();                        
                            var dataTable = new DataTable();

                            cancellationToken.ThrowIfCancellationRequested();

                            dataAdapter.Fill(myTable);
                            return myTable;
                        }
                    }
                }
                return null;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }

private void cancelOperation_Click(object sender, RoutedEventArgs e)
{
     cts.Cancel();
}

我尝试在DataTable RunDLAsync方法中添加取消令牌,但无法使其正常工作。

0 个答案:

没有答案