1

I have a Winform application which does several queries to Excel-Datatables with oleDB. It works so far, but now I want to add a Progressbar which inform the User that the application is not hanging up but loading the necessary informations based on the user input.

Using Backgroundworker and a new task seems like that the progressbar and the whole UI freeze.

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();
}

EDIT:

I believe the problem is, that Im setting the datagridview.datasource = Datatable while the progressbar is running, so everything is unresponsive. So what I want is, that the Backgroundworker is running the data manipulation and put the final datatable as datasource, while it is running, the progressbar should just continueing this marquee. If the backgroundworker has finished, then the progressbar should just disappear...

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;
    }
V.Hunon
  • 320
  • 3
  • 12
  • Are you using a BackGroundWorker or a Task? Did you mean that using one **OR** the other has the same effect? You know you cannot update an UI element from a thread other than the UI thread without invoking the UI thread. If you do that too often, the UI thread will be blocked anyway. So, arrange the proc to update once in a while. Make your `Button.Click` event async and await the Task(s) (`await Task.WhenAll()`). – Jimi Apr 05 '19 at 03:02
  • You are blocking with `Task.WaitAll`. Try `await` after marking the method `async`. Also, you cannot update the GUI from a background thread. – Zer0 Apr 05 '19 at 03:04
  • https://stackoverflow.com/a/13874504/10344668 – TerribleDog Apr 05 '19 at 03:38
  • I don't see any `BackgroundWorker` in your code only `Task`. **Don't combine the two**. If you are new to coding then I would recommend you just stick with `BackgroundWorker` as it has _buit-in_ support for reporting progress with WinForms –  Apr 05 '19 at 04:09
  • I tried both and both works for updating my Datagridview. But I don't get the Progressbar as I would like to – V.Hunon Apr 05 '19 at 05:26
  • @TerribleDog, I saw that already. I don't have a loop since I am only querying for a Datatable in Excel. So how should I update my progress then? – V.Hunon Apr 05 '19 at 05:27
  • @V.Hunon A progressbar can show it's progress on different ways not just from 0 to the last number. It can also be continuous. – TerribleDog Apr 05 '19 at 05:33
  • and how do I do that? Forexample I am getting data from a datatable in excel and call it via oledb, I try it with invoke but that wont work either... – V.Hunon Apr 05 '19 at 05:56
  • See: [BackgroundWorker.WorkerReportsProgress](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker.workerreportsprogress) and the sample code you find there. – Jimi Apr 05 '19 at 07:16
  • I've read it but still don't get it. – V.Hunon Apr 05 '19 at 07:47

0 Answers0