Detect restart in Windows 8 - SessionEnding not called - c#

When the user shuts downs Windows 8 from the Settings charm, my WPF application can detect that with the SessionEnding event.
In the case of choosing Shut down, I get "Session Ending Due To SystemShutdown"
SystemEvents.SessionEnding += SystemEvents_SessionEnding;
private void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
{
Trace.WriteLine("Session Ending Due To " + e.Reason);
}
Shut down calls SessionEnding, but Restart does not
But when the user chooses Restart then SessionEnding is not called!
Detecting Restart intent in Windows 8
How can it be done?

Here they say that by watching for WM_EndSession message you should be able to watch for a reboot: http://www.autoitscript.com/forum/topic/94616-detect-windows-shutdownlogoffrestart-event/
You will also need to hook to WndProc in your WPF application : How to handle WndProc messages in WPF?

Related

Trigger suspension events in a Windows Store app App 8.1

How to trigger suspension of events in a Windows Store app App 8.1 after being idle for a certain period of time? Or simply how to suspend it after being idle for a certain period of time?
You can't force suspend the app, you can terminate it, it'll be suspended automatically after a period of time.. but you can trigger that by subscribing to OnSusbended Event in app.xaml.cs:
this.Suspending += OnSuspending;
Handler:
private void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
//Use deferral for await calls :)
deferral.Complete();
}
Btw, you can debug it using VS, refer to this:
https://msdn.microsoft.com/en-us/library/hh974425.aspx
UPDATE:
Because the app is Suspended, you can't run a code to check, but what you can do is: Save a value to the settings in your app once the app is suspended, let's say it's: IsSuspended = 1, and make it 0 whenever the app rises OnActivated Event, this is a good way to check from a BackgroundTask or your foreground app after resuming.

PushNotificationReceived event doesn't fire

I am developing Windows Phone 8.1 app and I have this issue.
I don't want my push notifications to pop when the app is in foreground - so when the notification received from PushNotificationChannel I am checking if the app is in the foreground on PushNotificationReceived event, if so, I am canceling the notification by:
args.
void channel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
{
if(appInForeground)
{
args.Cancel = true;
}
}
My PushNotification background task (IBackgroundTask) trigger is PushNotificationTrigger.
The issue:
Sometimes when the app is in foreground - I am receiving push notifications. When I look into logs I can see that PushNotificationReceived event didn't fire.
I am not unsubscribing form the event in my whole app life...
Can it be possible that the event didn't fire but my backgroundTask trigger did?
Something is fishy here...
Any help will be appreciated.
There is an option in the push notificaton system of windows to suppress popup when the app is in foreground. So basically the xml for the push notification should have this property defined when sending a push to device.
As of Windows Phone 8.1, the phone can also keep a toast notification from being displayed through use of the ToastNotification.suppressPopup or ScheduledToastNotification.suppressPopup property in the notification's XML content.
More details present in this link here . Hope this helps!

Minimize Event ( C# Win 8.1 app)

I'm developing an App for windows 8.1 and i need to execute a method to pause some tasks or play a sound when the app is minimized in the sidebar.
I tried:
Application.Current.Suspending += new SuspendingEventHandler(Method_Name);
private void Method_Name(object sender, object e)
{
Other_Method();
}
But this event takes a few seconds (5~10) to occur after I minimize the app.
What event occurs when the app is dragged to the sidebar? What process sends the event?
Thanks.
Check out this post for the answer. It's WindowSizeChanged and check the value of ApplicationView.Value.
[EDIT]
Apparently for 8.1 things have changed a bit. The ApplicationView stuff is deprecated (that was quick) but this still takes place in SizeChanged for the window. Check out this for more details.
After long searching I found something that is not exactly what i wanted, but it works.
This event occurs everytime that visibility of the page changes (Is started, is maximized or minimized for exemple), then you must do some conditions using the if operator.
Window.Current.VisibilityChanged += new WindowVisibilityChangedEventHandler(One_Method);
private void One_Method(object sender, Windows.UI.Core.VisibilityChangedEventArgs e)
{
if(Some_Condition)
{
//TODO: Code that you want execute.
}
}
I'll keep the answer in open for the case of someone knows something more efficient.

Alarm without message dialog

I would like to make and App that when users set an alarm (like wake up alarm) and alarms sounds, doesnt show a message dialog to confirm and stop the alarm.
On web I have discovered some methods called Obscured and UnObscured that fired when the message's alarm shows, but the message still showed!
In Code:
public OverAlarmPage()
{
InitializeComponent();
PhoneApplicationFrame phoneAppRootFrame = (Application.Current as App).RootFrame;
phoneAppRootFrame.Obscured += OnObscured;
phoneAppRootFrame.Unobscured += Unobscured;
}
void OnObscured(Object sender, ObscuredEventArgs e)
{
txtStatus.Text = "Obscured event occurred";
}
void Unobscured(Object sender, EventArgs e)
{
txtStatus.Text = "Unobscured event occurred";
}
I just want to make stop the alarm sound with the vibrating of mobile (with gyroscope) but without the hell message!
There are any solution or is this a O.S feature that i cant touch?
The built in notification classes (Alarm and Reminder) don't support any customization of the UI that is displayed when they are triggered.
The only alternative I can think of would require the application to remain running (possibly under the lock screen) and then you just play the sound from the app at the appropriate time.
Obviously this requires that the app remain running though.
Other than that you will be prevented from trying to create the behaviour you are after.

SystemEvents.SessionEnding not firing

I am developing an windows forms application in c# .net 4.0.
I want to capture windows logoff event.
Here is the code:
public Form1()
{
InitializeComponent();
SystemEvents.SessionEnding += (s, e) =>
{
if (e.Reason == SessionEndReasons.Logoff)
{
MessageBox.Show("LogOff");
}
else if (e.Reason == SessionEndReasons.SystemShutdown)
{
MessageBox.Show("ShutDown");
}
};
}
Why isn't my sessionEnding firing?
It depends on the configuration that is set on gpedit.msc.
Open gpedit.msc, navigate to Computer Configuration > Administrative Templates > System > Shutdown Options and choose Turn off automatic termination of applications that block or cancel shutdown. Since mine laptop configure make it automatic shutdown, so it will never fire session ending
Perhaps you can move your code above into entry point of its windows (in the main).
Perhaps you can override windows message. You can see it in MSDN library documentation.
http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.sessionending.aspx
Shutdown message pump has been re route by other software and not re route to your apps
This could be useful to someone.
if form close event is included
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
then SessionEnding will not be fired, i just encoutered this problem and rectified it
void SystemEvents_SessionEnding(object sender, Microsoft.Win32.SessionEndingEventArgs e)
{}
here i need to prevent Form close upon Alt+F4 command, so i added this form closing event this resulted in this issue. so we can integrate session ending event in form close event. Option 2 in Refered from stackoverflow answer

Categories

Resources