WPF Windows Not Responding After Windows Sleep/Resume - c#

I have a fairly simple C# WPF application using .NET 5. Basically it sits in the background and times specific events for the end user. The events are built from a xml file that is generated externally.
The application consists of 2 windows, one hidden that does all the thinking. If it detects that an event is due it raises a toast message which when clicked on opens the other window to show the event details to the user. All works fine and runs as expected except after a windows sleep/suspend and resume. We obviously don't want the events to add up upon sleep/suspend and so we close the hidden window and upon resume open it again. No problems there but once the system is resumed and an event is raised the visible window refuses to show. If the visible window is open when sleep/suspend happens then upon resume the whole window is frozen and refuses to respond (only way to close the window is kill the application and restart)
The APP code is as follows :-
public static Forms.NotifyIcon notifyIcon;
public static MainWindow mw;
public static ConfigWindow cw;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged);
// Listen to notification activation
ToastNotificationManagerCompat.OnActivated += toastArgs =>
{
// Obtain the arguments from the notification
ToastArguments args = ToastArguments.Parse(toastArgs.Argument);
// Obtain any user input (text boxes, menu selections) from the notification
ValueSet userInput = toastArgs.UserInput;
// Need to dispatch to UI thread if performing UI operations
Application.Current.Dispatcher.Invoke(delegate
{
ToastControl.HandleToast(args);
});
};
ConfNotifyIcon();
OpenApp();
}
private void ConfNotifyIcon()
{
notifyIcon = new Forms.NotifyIcon();
notifyIcon.Icon = new System.Drawing.Icon("Images/Wellformation.ico");
notifyIcon.DoubleClick += OnClick;
notifyIcon.ContextMenuStrip = new Forms.ContextMenuStrip();
notifyIcon.ContextMenuStrip.Items.Add("Open", System.Drawing.Image.FromFile("Images/Wellformation.ico"), OnClick);
notifyIcon.ContextMenuStrip.Items.Add("Close", System.Drawing.Image.FromFile("Images/Wellformation.ico"), OnClose);
notifyIcon.ContextMenuStrip.Items.Add(new Forms.ToolStripSeparator());
notifyIcon.ContextMenuStrip.Items.Add("Exit", System.Drawing.Image.FromFile("Images/Wellformation.ico"), OnExit);
}
private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
switch (e.Mode)
{
case PowerModes.Suspend:
this.Dispatcher.BeginInvoke((Action)(() =>
{
PrepareLock();
}), null);
break;
case PowerModes.Resume:
this.Dispatcher.BeginInvoke((Action)(() =>
{
PrepareAwake();
}), null);
break;
default:
break;
}
}
private void PrepareAwake()
{
OpenApp();
ConfNotifyIcon();
notifyIcon.Visible = true;
}
private void PrepareLock()
{
notifyIcon.Dispose();
cw.Close();
}
private void OnExit(object sender, EventArgs e)
{
Application.Current.Shutdown();
}
private void OnClose(object sender, EventArgs e)
{
mw.Close();
}
private void OnClick(object sender, EventArgs e)
{
OpenMain();
}
private void OpenMain()
{
mw = new();
mw.Show();
mw.Activate();
}
public static void OpenApp()
{
cw = new ConfigWindow();
}
The hidden Window XAML is as follows :-
<Window x:Class="WellformationDesktopApplication.ConfigWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WellformationDesktopApplication"
mc:Ignorable="d"
Title="ConfigWindow" Height="1" Width="1" Visibility="Hidden" WindowState="Minimized">
<Grid>
</Grid>
</Window>
with code as follows :-
Timer at = new();
public ConfigWindow()
{
BuildConfig();
InitializeComponent();
}
public void refreshconfig()
{
myObjects.Clear();
myObjects = NudgeManager.GetNudges();
NudgeHandler(myObjects);
}
public void BuildConfig()
{
myObjects.Clear();
myObjects = GetEvents(); // pulls a list of event names with intervals from the config file
EventHandler(myObjects); //Goes through the list of events and figures out when the next event is due based upon the interval in the configuration
ActionTimer();
}
private void ActionTimer()
{
at.Interval = 60000;
at.Elapsed += ChecktActions;
at.AutoReset = true;
at.Enabled = true;
}
private void ChecktActions(object sender, ElapsedEventArgs e)
{
//Go through the trigger times for all events and see if those time have passed, if they have raise a toast showing the event name.
//If an event is raised reset the trigger time for the event based upon the interval and reset that time.
}
and the visible window XAML is as follows :-
<Window x:Class="WellformationDesktopApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WellformationDesktopApplication"
mc:Ignorable="d"
ResizeMode="NoResize"
WindowStyle="None"
Title="MainWindow" Height="500" Width="800" Background="{x:Null}" Foreground="{x:Null}" AllowsTransparency="True">
<Grid x:Name="BG">
<TextBlock x:Name="Display" HorizontalAlignment="Left" Margin="546,13,0,0" Text="Show event name and appropriate information about the event here..." VerticalAlignment="Top" FontSize="22"/>
</Grid>
</Window>
With Code as follows :-
public MainWindow()
{
InitializeComponent();
setstyles();
this.MouseLeftButtonDown += delegate { DragMove(); };
}
We know that everything to do with the ConfigWindow works fine, we know it is closed upon suspend and a new one is opened on resume with new timings set and all the appropriate alerts working.
The issue is with MainWindow as after a suspend and resume it cannot be interacted with at all. The open button on the icon does nothing, if the window is opened is is completely frozen and cannot be interacted with in any way, and if a toast is clicked on the window does not open but the rest of the toast handling code works fine around it. This happens on Win8, Win10 and Win11.
Any help out there as I am completely at a loss for how this is happening?
Thanks

After much work and going through the code section by section commenting it out to see if it made a difference I have found the issue.
Buried deep inside the the code for the hidden window (4 calls to functions down the line) I found that the EventHandler() was also raising a listener for
SystemEvents.SessionSwitch += new SessionSwitchEventHandler(OnSessionSwitch);
With all the associated functions buried in a separate class that was not directly referenced from the window itself.
When this line was commented out everything worked fine, with it in and attached to the hidden window after Suspend/Resume of windows no UI changes would take place throughout the whole code (hence hidden window continued to working completely fine as it did not interact with the UI).
By lifting this code out into the APP space and handling it there rather than in a window the problem has gone away (though has revealed other issues that were not being handled upon Resume of Windows that I now have to fix).
So the answer is that for a WPF application listeners for SystemEvents of any type need to be housed int the APP code space and not within windows.

Related

What is the previous window called in WPF

I know the current window can be used with "this" but is there anything I can use to call the previous window?
For example I have this code going off when I press a button
Buyer_Login BuyerWindow = new Buyer_Login();
Visibility= Visibility.Hidden;
BuyerWindow.Show();
I need to be able to go back to the first window and I need to close the BuyerWindow and I was going to do it with this.Close();
What can I do to make the first window's visibility visible again?
You could handle the Window.Closed event:
MainWindow.xaml.cs
private void OnClick(object sender, EventArgs e)
{
var loginWindow = new BuyerLogin();
loginWindow.Closed += OnBuyerLoginWindowClosed;
this.Visibility = Visibility.Hidden;
loginWindow.Show();
}
private void OnBuyerLoginWindowClosed(object sender, EventArgs e)
=> this.Visibility = Visibility.Visible;
You should consider to show the login window from the App.xaml.cs before you show your main window (recommended):
App.xaml.cs
private async void App_OnStartup(object sender, StartupEventArgs e)
{
var loginWindow = new BuyerLogin();
bool? dialogResult = loginWindow.ShowDialog();
if (dialogResult.GetValueOrDefault())
{
var mainWindow = new MainWindow();
mainWindow.Show();
}
}
App.xaml
<Application Startup="App_OnStartup">
<Application.Resources>
</Application.Resources>
</Application>
There is a collection of open windows.
App.Current.Windows;
It depends on exactly what you're doing opening windows.
If you start up then mainwindow will be [0] in that collection.
Say you then open an instance of window1.
That in turn opens an instance of window2.
There is a bit of a complication if you f5 in visual studio because it opens adorner windows.
Setting that aside for a moment.
When I write code to do what I describe above.
In Window2 I handle content rendered:
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
}
private void Window_ContentRendered(object sender, EventArgs e)
{
var wins = App.Current.Windows;
wins[1].Close();
}
}
That instance of Window1 is closed.
Your new window is very likely the last window in that zero based collection and the previous one the window before that.
You could perhaps search the collection and find index for "this" and subtract one if you're doing more complicated things.
The chances are though, you want to close the window indexed by the count of that collection minus 2. Because it's zero based.
With my exploratory code, window1 closes with this:
private void Window_ContentRendered(object sender, EventArgs e)
{
var wins = App.Current.Windows;
wins[wins.Count - 2].Close();
}
Personally, I prefer single window apps and switch out the content in part of mainwindow. Leaving navigation buttons etc static in mainwindow.
If you're effectively opening one other window and closing the previous to do things then maybe you could consider a single window app instead.

WebView2 - intercept and prevent/cancel anchor navigation

In my WinForms app I used WebView2 to show thrird-party content.
When user clicks the link (or other navigation element) inside the webView, the target link should be opened in the default browser (not in the webView).
For most cases I was able to implement desired behaviour with NewWindowRequested and NavigationStarting events.
But for anchor navigation inside the current page (<a href="#my_anchor">) these events are not firing.
There is a SourceChanged event, which is fired in case on anchor navigation, and it even allows to determine that this is in-page navigation by checking IsNewDocument, but it does not allow to cancel the navigation.
I was thinking about js-based solution, something like subscribing to click events for all a tags or even for whole document with further filtering. But I realized that such a solution will not work in many non-trivial cases, including
dynamically created a elements
keyboard navigation (TAB to select link, Enter to initiate action, so no click event)
the cases when there are a lot of elements (img, etc) inside a element and user clicked on such an internal element
when navigation is initiated with js
So, is the way exists to handle and cancel any navigation, including anchors in the current page?
I had the same request. I'm using Win32 C++, and for me the NewWindowRequested event does fire on anchor navigation, and I added this implementation to cancel the default behaviour and open the Uri in the default browser instead...
// Register a handler for the NewWindowRequested event.
CHECK_FAILURE(m_webView->add_NewWindowRequested(
Callback<ICoreWebView2NewWindowRequestedEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2NewWindowRequestedEventArgs* args) {
// get the target Uri
LPWSTR sUri;
CHECK_FAILURE(args->get_Uri(&sUri));
// and if it was user initiated (e.g. click on an anchor tag)
BOOL bUserInit;
CHECK_FAILURE(args->get_IsUserInitiated(&bUserInit));
if (bUserInit) {
// cancel default behaviour
args->put_Handled(TRUE);
// and open Uri in default browser
ShellExecute(0, 0, sUri, 0, 0, SW_SHOW);
}
return S_OK;
})
.Get(), nullptr));
I also faced this issue, as a workaround I've made this:
//MainWindow.xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Name="MainWindow"
x:Class="WebBrowser.MainWindow"
mc:Ignorable="d"
FontSize="14" Title="WebView2 Test" Width="400" Height="400" WindowStartupLocation="Manual"
SizeToContent="Manual" Cursor="Wait"
WindowStyle="None" ResizeMode="NoResize" PreviewKeyDown="AdvertisementWindow_PreviewKeyDown">
<DockPanel>
<wv2:WebView2 Name="myWebBrowser"/>
</DockPanel>
</Window>
MainWindow.xaml.cs Source code
using System.Windows;
using System.Windows.Input;
using Microsoft.Web.WebView2.Core;
namespace WebBrowser
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private string url = "https://en.wikipedia.org/wiki/Hyperlink";
public MainWindow()
{
InitializeComponent();
InitializeAsync();
myWebBrowser.NavigationStarting += MyWebBrowser_NavigationStarting;
myWebBrowser.SourceChanged += MyWebBrowser_SourceChanged;
}
private void MyWebBrowser_SourceChanged(object sender, CoreWebView2SourceChangedEventArgs e)
{
string uri = myWebBrowser.Source.ToString();
if (e.IsNewDocument == false && uri.EndsWith("#Further_reading"))
{
MessageBox.Show("You clicked #Further_reading", "Further Reading");
}
}
async void InitializeAsync()
{
await myWebBrowser.EnsureCoreWebView2Async(null);
myWebBrowser.CoreWebView2.Navigate(url);
}
private void AdvertisementWindow_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if(e.Key == Key.Escape)
{
Close();
}
}
private void MyWebBrowser_NavigationStarting(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationStartingEventArgs e)
{
//cancel the current event
if (!e.Uri.ToString().StartsWith(url))
{
e.Cancel = true;
System.Diagnostics.Process.Start(e.Uri.ToString());
}
}
}
}
This code should open each link in an external browser (the default system browser) and anchors to external pages will be opened in the external browser too. So it means that we cancel navigating in the WebView (e.Cancel = True). Regarding anchors to the same site, we can handle them in the SourceChanged event, and we don't need to stop navigating as it will stay on the same page.

WPF Application Shutdown When Exception Occurs in WindowClosing Method

I find when an exception occurs in MainWindow#Window_Closing method, even I set the e.cancel = true before the exception and e.handled = true in App#DispatcherUnhandledException, the application still shutdown.
I set a breakpoint in Application_Exit method and the stacktrace shows the shutdown is called by ExceptionWrapper.InternalRealCall -> Application.ShutdownCallback -> Application.ShutdownImpl -> Application.DoShutdown
Could anybody help me understand why this happens and is there any way to avoid application shutdown when exception happens in Window_Closing method? (I know I can always catch all exception in this method but I'd like to know if there is any better approach to avoid unexpected exception kill the application)
Below is sample code to reproduce this issue. Click the window close button to reproduce this issue.
Thanks.
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
throw new Exception();
}
}
App.xaml.cs
public partial class App : Application
{
private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
e.Handled = true;
}
private void Application_Exit(object sender, ExitEventArgs e)
{
var exitCode = e.ApplicationExitCode;
}
}
App.xaml
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
StartupUri="MainWindow.xaml" DispatcherUnhandledException="Application_DispatcherUnhandledException" Exit="Application_Exit">
MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Closing="Window_Closing">
It's fine catching the DispatcherUnhandledException, but that's not the end of it. If you catch it and set Handled to true, then the application just says OK, and shuts down. If Handled is set false, it says 'Still needs a bit of a check' and then if the AppDomain UnhandledException is set it calls that:
In your App.xaml.cs file:
private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
e.Handled = false;
}
private void Application_Startup(object sender, StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show(string.Format("Is Terminating: {0}\r\n{1}", e.IsTerminating, e.ExceptionObject.ToString()));
}
Here the CurrentDomain_UnhandledException method is only called is I set Handled to false. Either way, the apps going down.
Note that UnhandledExceptionEventArgs doesn't have a Handled property, but it does have an IsTerminating one. I'm not sure under what circumstances that would be false, though.
EDIT - Turns out that IsTerminating will only be false when compatibility is set to .NET 1.0 and 1.1 as in those versions of the framework an unhandled exception in some background threads would not cause the app to close. Since 2.0, all unhandled exceptions will cause the app to close.
If you look at the WPF source, you will see that this is by design. There is even an explicit comment to that effect:
// Event handler exception continuality: if exception occurs in Closing event handler, the
// cleanup action is to finish closing.
CancelEventArgs e = new CancelEventArgs(false);
try
{
OnClosing(e);
}
catch
{
CloseWindowBeforeShow();
throw;
}
I guess it makes sense, since you wouldn't want a bug to prevent the user closing a window.
I came across this few days back during one of my WPF projects. Yes, it is possible to prevent the application from shutting down on unhandled exceptions. Just let the application know that the exception is handled so that the application does not take additional actions. Makes sense right?
Wherever you initialize the application or have access to the Application Instance, add an event 'DispatcherUnhandledException' to the object of the instance. You will preferably have to add the event invoke function to App.xaml.cs of your WPF project.
Add this to the Constructor of App.
Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
Then add the Current_DispatcherUnhandledException function within the same class.
public static void Current_DispatcherUnhandledException (object sender, DispatcherUnhandledExceptionEventArgs e)
{
var errorBox = new Core.Common.UI.WPF.MessageBox();
errorBox.Error(e.Exception.Message);
//Log to Database
var logger = Logger.NLogger.GetLogger("My-Logger");
logger.Error(e.Exception, e.Exception.Message);
e.Handled = true;
}
You could also Log the exceptions externally to DB, File System.
e.Handled = true; This line tells the application that the exception is handled and not to worry about it.
PRO TIP - Make sure no exceptions happen within the DispatcherHandledException, else the Application will still close down.

ShowDialog() ends prematurely if called from hidden windows

Consider this code:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Title = DateTime.Now.ToString();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Hide();
new MainWindow().ShowDialog();
Show();
Debug.WriteLine(Title);
}
}
The XAML is trivial:
<Window x:Class="ShowHide.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Button
Content="Test"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Click="Button_Click"/>
</Grid>
</Window>
If I go "up", it works as expected - this window gets hidden, the new one is created and is shown as a modal window. But when I close the currently visible window, all hidden windows "underneath" get shown at once and they're not modal anymore. Multiple lines appear in the output window.
However, if I comment out the Hide() call, the problem doesn't seem to arise, i.e. I get to close modal windows one by one in reverse.
To reproduce:
Run the code.
Press the "Test" button. The current window will hide and the new one will appear.
Press the "Test" button in the new window. Another window will be created again. You can repeat this step as many times as you want.
Close the window. Older windows will re-appear all at once and neither will be modal.
Is this by design? What would your workaround be?
This is by design, as you hide your Modal window and display it again using Show() rather than ShowDialog(), so you could modify your code as below:
bool isChild;
private void Button_Click(object sender, RoutedEventArgs e)
{
Hide();
new MainWindow() { isChild = true }.ShowDialog();
if (isChild)
{
ShowDialog();
}
else
{
Show();
}
}
The code works just as you write. You call a method Show(), but you should write ShowDialog(). Just change this code:
Hide();
new MainWindow().ShowDialog();
Show();
To:
Hide();
new MainWindow().ShowDialog();
ShowDialog();

How to make a messagebox show up on left click

I have a pretty simple problem but I can't get it to work. I want a MessageBox to appear each time I left click inside my form. I didn't know how to capture it on the whole form so I started of trying to capture my left click inside my WebBrowser1. However, nothing really happens when trying to trigger the event.
I declared the action as WebBrowser1_Mousedown.
private void WebBrowser1_Mousedown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
MessageBox.Show("test");
}
}
What am I doing wrong?
My relevant XAML as follows:
<Window x:Class="IndianBrowser.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="488.806" Width="807.089" MouseDown="Window_MouseDown">
and now trying with the webbrowser:
<WebBrowser x:Name="WebBrowser1" HorizontalAlignment="Stretch" Height="auto" Margin="0,85,0,0" VerticalAlignment="Stretch" Width="auto" MouseDown="WebBrowser1_Mousedown"/>
If you look into MSDN documentation for WebBrowser class, you'll see that mouse events are not supported. What you can do instead is subscribe for HtmlDocument.MouseDown event.
Update
Here is small snippet that demonstrates how to do this in WPF, NOTE you will have to add reference to Microsoft.mshtml assembly:
public MainWindow()
{
InitializeComponent();
this.webBrowser1.Navigated += webBrowser1_Navigated;
this.webBrowser1.Source = new Uri("your url");
}
void webBrowser1_Navigated(object sender, NavigationEventArgs e)
{
HTMLDocumentClass document = this.webBrowser1.Document as HTMLDocumentClass;
document.HTMLDocumentEvents2_Event_onclick += document_HTMLDocumentEvents2_Event_onclick;
}
bool document_HTMLDocumentEvents2_Event_onclick(IHTMLEventObj pEvtObj)
{
// here you can check if the clicked element is your form
// if (pEvtObj.fromElement.id == "some id")
MessageBox.Show("test");
return true;
}

Categories