Skip to content

Commit b6b04bc

Browse files
vkuttypCopilot
andcommitted
feat: add onProgress callback to FillDataTableAsync
Optional Action<int> called on the UI thread after each batch with the running total of rows loaded. Useful for label/title updates and diagnosing large result set loading. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent d33d7b0 commit b6b04bc

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

src/SqlDotnetty.Core/ISqlDatabase.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,25 @@ public static System.Data.DataRow ToDataRow(this SqlRow row)
173173
/// <param name="rows">The async row stream from <c>QueryStreamAsync</c>.</param>
174174
/// <param name="table">A DataTable already bound to a DataGridView (or similar).</param>
175175
/// <param name="batchSize">Rows to accumulate before flushing to the DataTable. Default 500.</param>
176+
/// <param name="onProgress">
177+
/// Optional callback invoked on the UI thread after each batch is flushed.
178+
/// Receives the total number of rows added so far. Use to update a label, progress bar, etc.
179+
/// </param>
176180
/// <param name="ct">Cancellation token.</param>
177181
/// <example>
178182
/// <code>
179183
/// var dt = new DataTable();
180-
/// dataGridView1.DataSource = dt; // bind upfront
184+
/// dataGridView1.DataSource = dt;
181185
///
182186
/// await conn.QueryStreamAsync("SELECT * FROM BigTable")
183-
/// .FillDataTableAsync(dt); // rows appear in batches, form stays responsive
187+
/// .FillDataTableAsync(dt, onProgress: n => label1.Text = $"{n} rows loaded");
184188
/// </code>
185189
/// </example>
186190
public static async Task FillDataTableAsync(
187191
this IAsyncEnumerable<SqlRow> rows,
188192
System.Data.DataTable table,
189193
int batchSize = 500,
194+
Action<int>? onProgress = null,
190195
CancellationToken ct = default)
191196
{
192197
// Bounded channel: producer (background) reads network; consumer (UI thread) flushes batches.
@@ -225,9 +230,12 @@ public static async Task FillDataTableAsync(
225230

226231
// Consumer: runs on the calling (UI) thread — no ConfigureAwait(false).
227232
// await Task.Yield() between batches lets the message pump process redraws/clicks.
233+
int totalRows = 0;
228234
await foreach (var batch in channel.Reader.ReadAllAsync(ct))
229235
{
230236
FlushBatch(table, batch);
237+
totalRows += batch.Count;
238+
onProgress?.Invoke(totalRows);
231239
await Task.Yield();
232240
}
233241

0 commit comments

Comments
 (0)