BackgroundWorker ReportProgress no loop just long database operation

c#, vb.net, winforms

Solution

You can use the Marquee style of the ProgressBar to show indeterminate length of an active process:

BackgroundWorker bgw = new BackgroundWorker();
bgw.DoWork += bgw_DoWork;
bgw.RunWorkerCompleted += bgw_RunWorkerCompleted;
progressBar1.Style = ProgressBarStyle.Marquee;
progressBar1.MarqueeAnimationSpeed = 50;
bgw.RunWorkerAsync();

void bgw_DoWork(object sender, DoWorkEventArgs e) {
  // long work
}

void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
  progressBar1.Style = ProgressBarStyle.Continuous;
  progressBar1.MarqueeAnimationSpeed = 0;
}

Problem

I have .NET 4.5 Windows Forms application where one of the methods takes a while to complete (it's a BulkCopy function which loads a considerable amount of data and pushes into SQL). I would like to use a BackgroundWorker and ReportProgress so the user will know that there is something going on. I made a few applications that use this but all of them are in some kind of a loop when the BackgroundWorker is doing work and I can easily ReportProgress inside each loop step. Here I have a problem because there is no loop, code steps would be: - worker start async - get data from DB2 into a datatable (this takes the most time) - SqlBulkCopy datatable into SQL table I would need to start reporting progress (albeit a fake progress percentage, a simple spinning progress bar would suffice) between step 1. and 2. and end reporting progress after step 3. Anyone had a similar problem/solution, I guess I could just display a GIF image and hide it after work is done, but I think this won't work as the form freezes (Not responding message).

Original source