Orion Edwards
Orion Edwards

Reputation: 123642

BitmapCache causes WPF application to lock up

Here's the scenario:

1: Our WPF application starts

2: The WPF application loads various user controls, including some that use the new BitmapCache feature in WPF 4

3: Hit ctrl-alt-delete or win+l or load another application which requires UAC

4: Cancel that stuff

The WPF application is now unresponsive, however it's WPF itself which is the problem. All the application logic on all the background threads is fine, and you can interact with it by pressing alt+f4, etc, but the screen never redraws

Points of note:

Help!!

Upvotes: 5

Views: 2312

Answers (3)

Cyclone
Cyclone

Reputation: 41

As this issue still exists after ~14 years, Here's a solid way to reproduce it.

  1. Create a window, Create another one, On second one add BitmapCache anywhere or on window itself.
  2. Now lock windows and cancel it, Second window will lock. I am deep diving to this issue and repo to produce it is avaiable here : https://github.com/CycloneRing/WpfBitmapCacheIssue

Here's what I found to this point :

  • It seems only the first window that is created will receive updates for BitmapCache
  • If you assign BitmapCache to first window as well, Problem will go away
  • The lock is caused by a failing WM_PAINT to second window and it keeps spammed until BitmapCache response.
  • If you resize first window or hover over a button inside it, Lock will be go away.

Upvotes: 0

Orion Edwards
Orion Edwards

Reputation: 123642

After much hard work, a co-worker finally managed to track this down.

Our application initially shows a logon window, then closes it, and shows a main window. We discovered a long time ago that if you do this, WPF assumes that the first window (the logon window) is the application's main window, and so it shuts down when it closes.

To mitigate this, we created a fake offscreen window (which is never shown) before any other windows. WPF then assumes this fake window is the main one - it never closes, so our app can work as desired.

For reference, the fake window looks like this:

m_mainWindow = new Window { Left = int.MaxValue, Top = int.MaxValue, Width = 1, Height = 1 };
m_mainWindow.ShowInTaskbar = false;

This was all fine. Some time later, another developer added some code to Show and Hide this fake window.

Showing the main window was what caused it.


Here's a full repro:

A: Make a new WPF app in visual studio

B: Put the following code in your App class in App.xaml.cs

public partial class App : Application
{
    Window m_mainWindow;

    public App()
    {
        m_mainWindow = new Window { Left = int.MaxValue, Top = int.MaxValue, Width = 1, Height = 1 };
        m_mainWindow.ShowInTaskbar = false;
        
        // Comment this line out and the problem goes away
        m_mainWindow.Show();
    }
}

C: Put at least one BitmapCached element in your MainWindow.xaml. Here's mine

<Window x:Class="LockupRepro.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid CacheMode="BitmapCache">
        <Button Content="Look a button" />
    </Grid>
</Window>
  1. Run this program (I'm on windows 7), and then press ctrl+alt+del, or bring up a UAC prompt.

You'll see the app appear to lock up as described in the question - commenting out the m_mainWindow.Show fixes it.

Update:

Upon some further investigation it's not just showing the window, it's the fact that ShowInTaskBar is set to false. If we don't set that, then showing the window no longer causes the issue.

Bizarre!

Upvotes: 2

ProfyTroll
ProfyTroll

Reputation: 391

Seems problem is hard to reproduce. All i can suggest is advanced debugging -

  • Ensure your stack trace browser "Shows external code" (right button menu in VS on stack trace). Then locate exact place in WPF internals where your application stuck.

  • Next step is to load pdb's for that assembly or much simpler - use .NET Reflector.

  • A lot of looking and thinking...

  • ???

  • PROFIT!

    :)

Upvotes: 0

Related Questions