1

I have a software in C# I'm writing and every time its doing a hard task and I switch windows to let it complete the window screws up. Not sure how to word it but all of the buttons disappear or become "holes" . I know the application is running because the progress bar shows up again after a while. How do I fix this? I've been searching and I'm sure it has something to do with doubleBuffering.

Cut off window

ebolton
  • 1,126
  • 3
  • 14
  • 20

2 Answers2

2

you normally solve this by executing your resource intensive process in a separated thread than the main UI thread, in this way the UI thread can refresh the UI as needed and your long lasting operation is completed in parallel. After the background / worker thread has completed its task the control flow will return to the application.

Things are a bit more complicated when you want to update the status bar in the UI thread from the worked thread, usually you have to use the Invoke methods because you definitely should not even try to access and modify UI controls from another thread.

a bit cheaper method which kind of works but can have some issues from time to time is to include in your long lasting operation a call to Application.DoEvents() from time to time, for example if you are in a loop, every few iterations of the loop (depends on how much time it takes to execute a single iteration and on how many iterations you have in total); this method works and saves you completely from start working with multiple threads but is also considered less reliable.

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
0

As LarsTech already pointed out, use the BackgroundWorker-Class, especially for tasks which take longer than just a few seconds.

Make sure to use ReportProgress in your Backgroundworker to notify your Progressbar.

Good links worth studying:

Fabian Bigler
  • 10,403
  • 6
  • 47
  • 70