Daniel Peñalba
Daniel Peñalba

Reputation: 31887

Small flickering when showing a modal dialog in .NET

I'm experiencing a annoying issue in my .NET Windows Forms application.

I have a MainForm (A) and a progress form (B) that is shown modal when I start a long operation.

When the operation finishes, and B is closed, it seems that the window that is behind my application (is occurs usually with Skype) is brought to front during few milliseconds and, then my application is activated normally.

Is only a small flickering, but annoying. Here I write some tips that could help to find the solution:

Upvotes: 7

Views: 4320

Answers (5)

prem
prem

Reputation: 3548

In my case I was also facing the same issue in my VB.Net winform application but a bit different scenario. I was having a user control which opens up a dialog using showdialog() say dialog1 and on filling some data it hides dialog1 and opens up dialog2 using showdialog() again.

In the process of hiding dialog1 and showing dialog2 flickering occurs and it shows the window at background for a moment.

After trying so many solutions and workarounds none work for me. I found one workaround myself which might help others.

To hide dialog1 I was using Me.Hide(), The solution is to change the opacity of form instead of calling Hide() method.

'Me.Hide()

Me.Opacity = 0

After this workaround the application works fine without any flickering issue.

PS: The above code lines are in VB.Net but enough for a .Net winform application developer to get the idea for fixing this issue.

Upvotes: 1

user448516
user448516

Reputation:

Check if you DON'T call Hide or Close. The only way to avoid flickering is DialogResult. Upd: this.DialogResult = DialogResult.Ok

Check handlers OnFormClosing and etc. They might containt wrong method call.

The little trick is to set Owner explicitly

_dialog.Owner = this;
_dialog.ShowDialog();

People who are editing the post - _dialog.ShowDialog(this) works a bit different. Look at owner = ((Control) owner).TopLevelControlInternal; in decompiled code

Edited by someone:

Or...

_dialog.ShowDialog(this);

These calls are identical according to MSDN

Upvotes: 4

Maciek Leks
Maciek Leks

Reputation: 1448

  1. Do not call Close for modal window (it will not be disposed and memory leak is guaranteed)

  2. Set this.DialogResult = DialogResult.OK

  3. Call Dispose() from the parent, NEVER from the form you are closing

  4. Remember of disposing all of your components holding by the form IContainer in Dispose(bool) (VS Designer implementation of Dispose(bool) is usually not enough not to have memory leaks)

Upvotes: 1

egelvin
egelvin

Reputation: 523

I was experiencing these same symptoms and it was driving me crazy.

I finally discovered the problem was that i was calling this.Dispose() instead of this.Close() to close the modal window I'm not sure why I called this.Dispose() in the first place.

After switching methods, the problem went away.

I realize that this thread is old and this is not the cause of your problem, i am just trying to help anyone else who made the same mistake that I did.

Upvotes: 4

ChrisLively
ChrisLively

Reputation: 88072

Okay, it sounds like

  1. In the main window a user starts a long operation
  2. You display a progress modal window
  3. Operation completes you close the progress window
  4. Your main window doesn't display immediately. Instead something behind it shows through for a second or less.
  5. Your main window completes it's redraw operation and is 100% visible.
  6. This happens more often when you have applications such as Skype running.

If that's the case there are many different possible causes. For example, your video drivers might have a bug in them causing delays in off screen compositing under certain conditions.

The system itself might even be experiencing a blocking CPU operation at the moment. This is something that could be caused by the time it takes your code to close the dialog and go back to the main form. You might look to see if there is anything else you are doing between the time the progress closes and you return UI control back to the user.

The system might simply be memory constrained and your operation causes a huge swap to disk. When the operation completes, windows might be notified that it needs to pull previously swapped memory from disk and shove it back into RAM, causing a delay.


I'd run the app with Nothing else loaded but task manager or resource monitor and see what happens. If the problem no longer occurs, then look into adding more RAM to your machine and/or ignore it.

If it's still occuring, and your memory usage leaves nearly nothing left then, again, add RAM or ignore.

If it's still occuring but memory usage is low, investigate your code to see what you are doing between the closing of the dialog and release of control of the main window back to the user.

Upvotes: 0

Related Questions