具有Backgroundworker和OLEDB Excel数据表的C#Winform Progressbar

时间:2019-04-05 02:51:44

标签: c# winforms progress-bar

我有一个Winform应用程序,该应用程序使用oleDB对Excel-Datatables进行了多个查询。到目前为止,它可以正常工作,但是现在我想添加一个进度条,通知用户该应用程序未挂起,而是根据用户输入加载了必要的信息。

使用Backgroundworker和新任务似乎会冻结进度栏和整个UI。

private void updateButton_Click(object sender, EventArgs e)
    {            
        progressBar1.Visible = true;
        progressBar1.Style = ProgressBarStyle.Marquee;

        Task t1 = Task.Factory.StartNew(() => QueryToExcel());
        Task.WaitAll(t1);
        progressBar1.Visible = false;

    }

private void QueryToExcel()
{
//doing some data manipulation with LINQ, oleDB etc. 
//and finally stored in datatable FinalTable
datagridview1.datasource = FinalTable;
datagridview1.Update();
}

编辑:

我相信问题是,我在进度条运行时设置了datagridview.datasource = Datatable,所以一切都没有响应。所以我想要的是,Backgroundworker正在运行数据操作并将最终数据表作为数据源,而在运行时,进度条应仅继续此选取框。如果背景工作者已经完成,那么进度条应该会消失...

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {            
        QueryToExcel();
    }

private void updateButton_Click(object sender, EventArgs e)
    {
        progressBar1.Visible = true;
        StartBackgroundWork();
    }

private void StartBackgroundWork()
    {
        if (Application.RenderWithVisualStyles)
            progressBar1.Style = ProgressBarStyle.Marquee;
        else
        {
            progressBar1.Style = ProgressBarStyle.Continuous;
            progressBar1.Maximum = 100;
            progressBar1.Value = 0;
        }
        backgroundWorker1.RunWorkerAsync();
    }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        progressBar1.Value = 0;
        dataGridView1.Update();
        progressBar1.Visible = false;
    }

0 个答案:

没有答案