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.
Related
I have a FileSystemWatcher that is waiting for files to appear in a folder, which then triggers an insert to an ObservableCollection. When the form is open on the screen the update happens successfully. But when the form is minimized I get a cross-thread exception? I know I can just check for context, but I want to know why this behaviour is happening so I can ensure it doesn't come up again in other places.
I've tried this with a new blank winform app, and it errors 100% of the time whether it is minimised or not, so I'm unsure what I'm doing wrong or how to diagnose the issue. The main app is thousands of lines long, with many custom libraries so I can't post all of it here.
Called by the watcher
private void CheckForFiles(object source, FileSystemEventArgs e)
{
WaitingFiles.Add(e.FullPath);
}
Which triggers this
private async void WaitingFiles_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
CurrentProcessStatusLabel.Text = "Checking download for despatch file";
}
When I minimize the window I run the following code;
Hide();
SysTrayIcon.Visible = true;
Unless a SynchronizingObject is set, events are raised on a background thread, and background threads are not allowed to update the UI. I'm not sure why it works unless the application is minimized, but a good start is to ensure you are using correct synchronization to avoid issues like this.
If you want to update the UI you should either set the SynchronizingObject to one of the controls in your UI, or manually schedule any UI updates to the UI thread.
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.
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?
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.
I'm experiencing odd behavior in the wp7 emulator.
I have a dead simple app that's mostly directly from the template generated by VS 2010.
From App.xaml:
<!--Required object that handles lifetime events for the application-->
<shell:PhoneApplicationService
Launching="Application_Launching" Closing="Application_Closing"
Activated="Application_Activated" Deactivated="Application_Deactivated"/>
Tombstoning code from App.xaml.cs:
private void LoadSettings()
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
ICollection<TicTacSpot> getSpots;
if (settings.TryGetValue<ICollection<TicTacSpot>>("spots", out getSpots))
{
Spots = getSpots;
}
if (Spots == null)
{
Spots = new List<TicTacSpot>();
}
}
private void SaveSettings()
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings["spots"] = Spots;
}
// Code to execute when the application is launching (eg, from Start)
// This code will not execute when the application is reactivated
private void Application_Launching(object sender, LaunchingEventArgs e)
{
LoadSettings();
}
// Code to execute when the application is activated (brought to foreground)
// This code will not execute when the application is first launched
private void Application_Activated(object sender, ActivatedEventArgs e)
{
LoadSettings();
}
// Code to execute when the application is deactivated (sent to background)
// This code will not execute when the application is closing
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
SaveSettings();
}
// Code to execute when the application is closing (eg, user hit Back)
// This code will not execute when the application is deactivated
private void Application_Closing(object sender, ClosingEventArgs e)
{
SaveSettings();
}
Seems straightforward enough. I set breakpoints in all these methods. When I hit F5 to deploy the app, the event handlers that are hit are:
Application_Launching()
Application_Deactivated()
Oddly, these are hit, even though the emulator doesn't show the app opening or closing.
In the emulator, I then open the app, play around, close it, then re-open it. I use both the "back" and "start" buttons to close it. Despite this, I am unable to get any event handlers to be hit again.
What am I doing wrong here?
Is the debug session is still active?
I have found that if you set breakpoints that get hit on start-up and you do not continue with a certain amount of time (e.g. < 10 seconds) your debug session will be disconnected.as the OS terminates the application.
Like Dennis said, to debug the Activated event handler you need to launch a new debug session just after pressing the back button. The sequence would be :
launch debug
play with the app, hit Start
quit application, debug session stopped
hit back button - black screen on the emulator
launch debug session , be quick :) after 10 sec, OS terminates the application