WPF UI Freezes after resuming from screen lock (Win+L) in Windows #559

Closed
swingfox opened this Issue Nov 23, 2016 · 25 comments

Projects

None yet

3 participants

@swingfox

I have a project with two Material Design WPF forms that will interact with each other. When I login to my first form it hides and the second form shows after I successfully logged in.

However when I perform a screen lock (Win+L) and resume my workstation, the UI freezes but I can receive notifications in my app and I can close it (using the close button) but my timer (label) in the UI and also other controls freezes.

I found out that the trigger of this issue is the hiding of my first form because I tried commenting the Hide() function in my first form and it went well. I tried to pass the first instance of my form to the second to solve it but it didn't went well.

I also recreated the steps and applied it to your sample project and the result is very similar when I do screen lock.

Why is it that the 2nd form freezes when I call Hide() in my first form? Any solutions for this bug?

Thanks

@punker76
Contributor

@swingfox If you use MahApps with this lib together then this issue is fixed in latest pre-release 1.4.0

@swingfox

@punker76 Thanks but in my case I use NuGet package manager in my project and I used the template of MainDemo.Wpf in MaterialDesignInXamlToolkit project samples. I didn't see any MahApps files in my project. How can I fix this?

@punker76
Contributor

@swingfox Then it must be something wrong with your code. You should provide the code with your two window logic.

@swingfox
swingfox commented Nov 23, 2016 edited

@punker76
Here is my code snippet of the two classes:

Note: mainWindow and login variable is a singleton instance of my class.

LoginForm.cs (1st form)

private LoginForm login;
public login()
{
	login = this;
	MainWindow.mainWindow.setJSONVisible(getUsers);
	MainWindow.mainWindow.SetLoginForm(login);
	MainWindow.mainWindow.Show();
}

MainForm.cs (2nd form)

private LoginForm login;
// This is triggered when the window's visibility is changed.
private void Window_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
       if (IsVisible)
       {
             Cursor = System.Windows.Input.Cursors.Wait;
             login.Hide();
       }
}

I tested several approaches just to pass my single instance, it works in every way except when I execute screen lock (Win+L) command in windows or the worst my app hangs when it is running all the time and then the UI suddenly blackens.

Additional info:
The MainDemo.Wpf project copy I used was dated back August 2016 (GitHub).

These are my dlls in NuGet right now:
MaterialDesignColors v. 1.1.3 July 6,2016
MaterialDesignThemes v. 2.2.0.725 November 4,2016

@ButchersBoy
Owner

@swingfox Please post a full GitHub repository, which is complete, compiles, and easily illustrates this problem.

There is nowhere near enough information to go on.

Have you even attempted to check for a deadlock involving the UI thread?

@ButchersBoy
Owner

@swingfox "the UI freezes but I can receive notifications in my app"

So you have some background thread/tasks going on, correct? You may want to take a look at something like this: https://msdn.microsoft.com/en-us/library/ms164746.aspx and take a look at the thread debugging tools available in VS.

@swingfox

@ButchersBoy I have a socket listener for my project. However, it is thread safe. When I tried their example by doing some screen locks when having a socket, it performs well. Unfortunately, the project is a company project I've been doing. I could only provide it at the basic level of bug demonstration. The bug still appears, even if I have no other threads other than the UI thread.

Here is the quick replication of the freezing problem:
https://github.com/swingfox/materialxamldesignUIfreeze

@ButchersBoy
Owner

OK, I have the repo loaded...how exactly do I reproduce the issue?

@swingfox

For the freezing test case scenario. Just click log in, then lock your computer screen (Win+L). Then resume your window, after that the timer freezes and some controls like back button except for the close button. The bug occurs when the user enters the screen lock state and also when the WPF program is running for a long time or idle (these cases rarely occurs).

@swingfox

@ButchersBoy @punker76 I found the line of code that causes the freezing of the UI. In the MainWindow.xaml I removed the tag:

<materialDesign:ColorZone Padding="16" materialDesign:ShadowAssist.ShadowDepth="Depth2" Mode="PrimaryMid" DockPanel.Dock="Top">
...
</materialDesign:ColorZone>

And the freezing gone! However my program's UI would be incomplete without it. When I tried to screenlock in my first form (LoginForm.xaml) it didn't freeze (I can still click the LOGIN button after screen resume) but when I do it in my MainWindow.xaml it freezes. What's your solution on this without removing some parts of my MainWindow.xaml UI?

@punker76
Contributor

@swingfox Ok think that's not the solution for the whole problem. I looked at your code and it feels like something is wrong with your code behind semaphore threading stuff. maybe a thread is blocked if you lock the screen.

@ButchersBoy
Owner

I might have discovered something a bit wierd... I'll report back later...

@swingfox

@punker76 The purpose for my semaphore at start up is to make my program ran singly. I tried removing the semaphore and ran the program. The bug still remains.

@swingfox

@ButchersBoy OK, when the "freezing after screen lock" is solved I think you could also solve the UI hanging mystery when the app is idle or stays open for a long time. I think there must be some link between those scenarios.

@swingfox

@ButchersBoy Hello, I would like to follow up about the issue because we would release our application next week. Thanks :)

@ButchersBoy
Owner

And I thought it was just me that had deadlines.

@swingfox

@ButchersBoy Okay, no worries. I'll just find a temporary workaround for this one.

@ButchersBoy
Owner

I do have a fix.

It will land soon.

@ButchersBoy
Owner

You need to test with nuget version 2.2.0-ci739

Add this attribute to your Window level elements:

materialDesign:ShadowAssist.CacheMode="{x:Null}"

@ButchersBoy ButchersBoy added this to the Version 2.2.1 milestone Nov 25, 2016
@swingfox
swingfox commented Nov 25, 2016 edited

Thank you very much man, you rock! One more thing, after I resume my screen lock. I got this UI scrollview with broken lines around its borders. The scroll view is updated everytime a client updates his status. How can I prevent this from happening everytime I resume from lock state?

@ButchersBoy
Owner

That's keyboard focus. I've no idea what element that is in your application.

@swingfox

Here's my sample output.

focus scrollview issue 2

@punker76
Contributor
punker76 commented Nov 25, 2016 edited

@swingfox You can remove this dotted border by setting FocusVisualStyle="{x:Null}" at the control which got the focus. And it seems this is your ListBox/ListView...

@swingfox

@punker76 @ButchersBoy Thanks a bunch on your feedback. That really solved a major bug on my project 👍

@punker76
Contributor

@swingfox The dotted focus border is not a bug, it's a WPF feature :-D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment