use backgroundworker progresschanged for using dataset without using loop
Loading
use backgroundworker progresschanged for using dataset without using loop
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Tahir AnsariPosted Aug 8, 2023, 2:50 PM
using System;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
namespace BackgroundWorkerExample
{
public partial class MainForm : Form
{
private BackgroundWorker backgroundWorker;
private DataSet dataSet;
public MainForm()
{
InitializeComponent();
// Initialize BackgroundWorker
backgroundWorker = new BackgroundWorker();
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.ProgressChanged += BackgroundWorker_ProgressChanged;
backgroundWorker.DoWork += BackgroundWorker_DoWork;
}
private void btnStart_Click(object sender, EventArgs e)
{
if (!backgroundWorker.IsBusy)
{
// Load your DataSet here (replace with actual logic)
dataSet = LoadDataSet();
progressBar.Value = 0;
progressBar.Maximum = dataSet.Tables.Count;
// Start BackgroundWorker
backgroundWorker.RunWorkerAsync();
}
}
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
// Process DataSet tables here
for (int i = 0; i < dataSet.Tables.Count; i++)
{
// Simulate some processing
System.Threading.Thread.Sleep(1000);
// Report progress to UI thread
backgroundWorker.ReportProgress(i + 1);
}
}
private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}
// Replace with actual DataSet loading logic
private DataSet LoadDataSet()
{
DataSet dataSet = new DataSet();
// Add DataTables and fill DataSet
return dataSet;
}
}
}