Displaying a progressbar while executing an SQL Query Displaying a progressbar while executing an SQL Query multithreading multithreading

Displaying a progressbar while executing an SQL Query


Use the BackgroundWorker class to fill your DataGrid.

     Form progressForm;     public void func() {        BackgroundWorker bw = new BackgroundWorker ();        bw.DoWork += new DoWorkEventHandler (bw_DoWork);        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler (bw_RunWorkerCompleted);        progressForm = new Form ();        ProgressBar pb = new ProgressBar ();        pb.MarqueeAnimationSpeed = 30;        pb.Style = ProgressBarStyle.Marquee;        pb.Dock = DockStyle.Fill;        progressForm.ClientSize = new Size (200, 50);        progressForm.FormBorderStyle = FormBorderStyle.FixedDialog;        progressForm.StartPosition = FormStartPosition.CenterScreen;        progressForm.Controls.Add (pb);        progressForm.ControlBox = false;        progressForm.TopMost = true;        progressForm.Show ();        string queryString = "SELECT ...."; // fill query string here        var params = new KeyValuePair<GridControl, string>(sorgu, queryString);        bw.RunWorkerAsync (params);    }    void bw_DoWork (object sender, DoWorkEventArgs e) {        KeyValuePair<GridControl, string> params = e.Argument as KeyValuePair<GridControl, string>;        ConnectionClass cc = new Connection Class();        cc.fillGrid(params.Value, params.Key);    }    void bw_RunWorkerCompleted (object sender, RunWorkerCompletedEventArgs e) {        progressForm.Close (); //    }

It is possible to send a parameter to the BackgroundWorker. If you need more than one parameter, you can send a Tuple which contains any objects you need.

EDIT: If you're on 3.5, you can use a KeyValuePair instead. Code is updated for that.


Just as Ash Burlaczenko recommended, you'll have to use a BackgroundWorker for that purpose.

Since, however, you'd like to tie it in with a ProgressBar, I'd recommend looking at this article on CodeProject: ProgressWorker.

It's fairly easy to use and it updates the progress bar for you automatically. All you'll have to do is remember to call the ProgressWorker.ReportProgress method from time to time in order to update the associated progress bar.