Example #1
0
 private void BeginExecuteReader(FillDataParam state)
 {
     Log("Running FileAsync...");
     OpenSelectConnection();
     Log("Beginning the asynchronous execution of the reader...");
     ((SqlCommand)SelectCommand).BeginExecuteReader(Execute, state);
 }
Example #2
0
        private void Execute(IAsyncResult result)
        {
            Log("Starting FillAsync invoke...");
            FillDataParam fillData = (FillDataParam)result.AsyncState;

            try
            {
                IsExecuting         = true;
                fillData.DataReader = ((SqlCommand)SelectCommand).EndExecuteReader(result);
                MinimalDataReader minimalReader = new MinimalDataReader(fillData.DataReader);

                Log("Starting initial table fills...");
                if (fillData.DataSet != null)
                {
                    Fill(fillData.DataSet, fillData.SourceTableName, minimalReader, 0, PageSize);
                }
                else
                {
                    Fill(fillData.DataTables, minimalReader, 0, PageSize);
                }

                if (FillInitialized != null)
                {
                    FillInitialized(fillData);
                    if (fillData.Cancel)
                    {
                        return; // Don't start the data retrieval
                    }
                }

                Log("Creating new background worker...");
                if (_workerThread != null)
                {
                    Log("Disposing former background worker...");
                    _workerThread.DoWork             -= new DoWorkEventHandler(FillDataSet);
                    _workerThread.RunWorkerCompleted -= new RunWorkerCompletedEventHandler(CompleteFillDataSet);
                    _workerThread.Dispose();
                }
                _workerThread                            = new BackgroundWorker();
                _workerThread.DoWork                    += new DoWorkEventHandler(FillDataSet);
                _workerThread.RunWorkerCompleted        += new RunWorkerCompletedEventHandler(CompleteFillDataSet);
                _workerThread.WorkerSupportsCancellation = true;

                Log("Starting new background worker...");
                _workerThread.RunWorkerAsync(fillData);
            }
            catch (Exception ex)
            {
                // Save the exception
                fillData.Error = ex;
                CompleteFillDataSet(this, new RunWorkerCompletedEventArgs(fillData, null, false));
            }
        }
Example #3
0
        private void FillDataSet(object sender, DoWorkEventArgs e)
        {
            Log("Starting FillDataSet invoke...");
            FillDataParam fillData = (FillDataParam)e.Argument;

            try
            {
                int startRecordIndex = fillData.StartRecord;
                int recordCount      = 0;

                Log("Starting internal fill while loop...");
                while (!fillData.DataReader.IsClosed && !_workerThread.CancellationPending)
                {
                    bool fillCompleted = false;

                    try
                    {
                        Log("Filling tables...");
                        if (fillData.DataSet != null)
                        {
                            recordCount = Fill(fillData.DataSet, fillData.SourceTableName, fillData.DataReader, 0, PageSize);
                        }
                        else
                        {
                            recordCount = Fill(fillData.DataTables, fillData.DataReader, 0, PageSize);
                        }

                        Log($"Tables filled. Record count={recordCount}");

                        fillCompleted = recordCount == 0 || recordCount != PageSize;

                        Log($"Fill completed boolean={fillCompleted}");

                        if (FillDataChunk != null)
                        {
                            FillDataChunk(fillData, startRecordIndex, recordCount);
                            if (fillData.Cancel)
                            {
                                _workerThread.CancelAsync();
                                SelectCommand.Cancel();
                                fillCompleted = true;
                            }
                        }

                        startRecordIndex += recordCount;

                        Log($"Next start instance: {startRecordIndex}");
                    }
                    catch (Exception ex)
                    {
                        fillCompleted = true;
                        throw new InvalidOperationException("Failed to fill data chunk.", ex);
                    }
                    finally
                    {
                        Log($"Closing resources.");

                        if (fillData.DataReader != null && fillCompleted)
                        {
                            fillData.DataReader.Close();
                        }
                        if (SelectCommand.Connection != null)
                        {
                            SelectCommand.Connection.Close();
                        }
                    }
                }
                IsExecuting = false;
            }
            catch (Exception ex)
            {
                fillData.Error = ex;
            }
            finally
            {
                e.Result = fillData;
            }
        }