Controls doesn't show if heavy process

79 views Asked by At

I use c# in winform.

Before a very heavy function, I want to show a waiting form to prevent the user. The form opens, but the controls in it are not drawn.

In the following code, waitingForm is a little form, with only a textbox and a progressbar

using (WaitingForm waitingForm = new WaintingForm())
{
  waitingForm.Show();
  HeavyFunction();
}

I see only two white rectangles, where the controls should appear. Why this happens ?

1

There are 1 answers

4
Patrick Tucci On BEST ANSWER

You can't show the waiting form before the heavy processing and expect the UI to remain responsive. The thread that you're clogging up with HeavyFunction() is the same one that is responsible for drawing forms, controls and maintaining the UI. Use a BackgroundWorker or the ThreadPool to offload the heavy processing to another thread.

EDIT: Also, please consider Ron Beyer's input regarding async/await