如何在后台工作程序中填充数据库中的数据表并在c#winform中显示进度?

时间:2018-01-20 11:54:31

标签: c# winforms progress-bar backgroundworker

我正在尝试从后台工作者的数据库填充DataTable,我想同时显示进度更新表单。 我知道我可以使用循环来显示进度,但是如何在填充表格时显示进度?

这是我用来以新形式显示进度条的代码:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            for (int i = 1; i <= 10; i++)
            {
                if (worker.CancellationPending == true)
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    // Perform a time consuming operation and report progress.
                    worker.ReportProgress(i * 10);
                    System.Threading.Thread.Sleep(500);
                }
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // Pass the progress to AlertForm label and progressbar
            alert.Message = "In progress, please wait... " + e.ProgressPercentage.ToString() + "%";
            alert.ProgressValue = e.ProgressPercentage;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled == true)
            {

            }
            else if (e.Error != null)
            {

            }
            else
            {

            }
            // Close the AlertForm
            alert.Close();
            button1.Enabled = true;
        }

0 个答案:

没有答案