Click here to Skip to main content
15,885,842 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
Today we found a little strange problem in one of our WPF application and we are not exactly sure how to deal with it. When we run the software on one of our laptops the screen goes to sleep (turns black) after a while when there is no user interaction. It application itself is still busy downloading data from the internet and filling tables in a database. There is also a progress bar and a status bar.
If we wake the screen simply by moving the mouse the program is shown again but some parts of the window are not refreshed. The progress bar is still showing values even if the process is completed and the status bar is also wrong.
From this time on you can work with the program like nothing happened but progress bar and status bar will only be redrawn if you minimize the complete window to the task bar and maximize it again. Or if you start another action that will also use progress bar or status bar.
This strange behavior caused a lot of confusion because the displayed data seems to be wrong after the screen awakes from sleep and you need to minimize and maximize the window to see what is really going on.
What is wrong here?
Posted
Updated 23-May-23 21:55pm
v2
Comments
Sergey Alexandrovich Kryukov 26-Apr-11 11:56am    
Interesting problem, my 5 for the question.
--SA

You need to handle the event Microsoft.Win32.SystemEvents.PowerModeChanged, see http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents(v=VS.100).aspx[^].

Look at this code sample: http://msdn.microsoft.com/en-us/library/hxkc1kwd.aspx[^].

For refreshing of the WPF Window try to call System.Windows.Window.UpdateLayout, see http://msdn.microsoft.com/en-us/library/system.windows.window.aspx[^].

Thank you for the interesting question again.

Good luck,
—SA
 
Share this answer
 
v4
Comments
Sparkling_ouc 26-Apr-11 21:27pm    
As you said , i tried a simple project just now:
///
/// Interaction logic for Window1.xaml
///

public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.Loaded += (o, e) => { this.TabControlMain.SelectedIndex = 0; };
SystemEvents.PowerModeChanged+=new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged);
}

private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
if (e.Mode == PowerModes.Suspend)
{
PrepareToSleep();
}
else
{
this.UpdateLayout();
}
}

private void PrepareToSleep()
{
this.TabControlMain.SelectedIndex = 1;
}
}

TabControlMain is a tabcontrol ,since the window loaded ,the selectedindex is 0,
when the computer went to sleep ,its selectedindex turned to 1,but after awaken, the UI didn't update ——after i minimized it and clicked it in the taskbar, then it appeared with selecting the tabcontrol's 1 index.

in other words, it seems that UpdateLayout doesn't work as I expected.

Thanks for your help.
Sparkling_ouc 26-Apr-11 21:29pm    
I tried it in the Winform Application ,it works well.
Sergey Alexandrovich Kryukov 26-Apr-11 21:58pm    
OK, it looks like you have the PowerModeChanged event working. (Did you really check it up?)

Interesting. Well, selection can be a different problem; I don't know the reason, it can be even a bug. You can work around: in prepare to PrepareToSleep remember selection (I don't know why you set selection, is it just for experiment?) and restore it on awaken. By the way, you should use PowerModes.Resume, not "else".

Also, I advise you use logging to debug it and know what exactly happens (use System.Event log and log into system log); log all key points. You cannot physically debug events like PowerModeChanged under debugger, by obvious reasongs :-)

--SA
Sparkling_ouc 26-Apr-11 22:18pm    
I changed the selected index is want to test whether the wpf application's UI would refresh after awaken or not.
yes ,PowerModeChanged it really worked, what doesn't work is the Window's UpdateLayout or maybe it works and not as expected.
Eealier I use Win32's API to get the Window's Sleep Message, PowerModeChanged worked same .
I will try to test as you advised.
Sparkling_ouc 26-Apr-11 22:46pm    
I tested just now with EventLog class .PowerModeChanged event really worked.
For whom is using the MaterialDesign libraries, it happens not only after going to sleep-mode but even after a simple screen-lock (Windows-L shortcut).
I solved adding this line to the Window element of the xaml file:
materialDesign:ShadowAssist.CacheMode="{x:Null}"

Hope it helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900