1

I have created a C# Winforms application.

In this application I opened one form in ShowDialog mode and when I click on the OK button then it starts the process of importing contacts from a GridView to the database (the number of rows may be up to 15,000).

The problem is that when the process of importing starts I increment the value of progressBar in a loop but if the form loses focus and I once again set the focus to that form then the progress bar is not updated.

How can I update the progress bar even when the form loses focus?

Ryan Shripat
  • 5,574
  • 6
  • 49
  • 77
Ayush
  • 165
  • 1
  • 3
  • 10
  • 1
    WinForms or WPF Application? ANYWAY answer is - do it from a separate thread, there are a lot of the same questions - TRY TO SEARCH – sll Oct 17 '11 at 11:12
  • Use a BackgroundWorker. Have a look [here][1]. [1]: http://stackoverflow.com/questions/1259949/how-do-i-implement-a-progress-bar-in-c – sloth Oct 17 '11 at 11:28

1 Answers1

2

Use Application.DoEvents() every now and then, but you really should use a BackgroundWorker to prevent this from happening.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • I suppose you've been downvoted because of `Application.DoEvents` – username Oct 17 '11 at 11:33
  • 1
    @username: Is `Application.DoEvents()` bad? It causes an otherwise 'unresponsive' application to respond from time to time. – Alex Essilfie Oct 17 '11 at 11:36
  • @AlexEssilfie In such cases there are much better ways to update UI. Take a look http://stackoverflow.com/questions/5181777/c-application-doevents – username Oct 17 '11 at 11:44
  • @username: Thanks for enlightening me. In my early days of coding, I'd use `Application.DoEvents()` just about everywhere. Thankfully, I never ran into any problems doing that. I unconsciously use threads now but never thought of any repercussions of using `Application.DoEvents()`. – Alex Essilfie Oct 17 '11 at 11:51