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
Related
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.
Hi my window form application have a dependency to file where it check the content and if the content is 0 then app should run otherwise it should message that instance is running fine. This is working fine when I am closing the app either by cross button form the form or form closed by its own (Environment.Exit(0);) but when I am closing the Window form application either by task manager or from the stop button of Visual studio ...the content of file is not changing to 0 . I have added the following code at:
private void Form1_FormClosing(Object sender, FormClosingEventArgs e)
{
File.WriteAllText(filename2, "0");
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
File.WriteAllText(filename2, "0");
}
And also before Environment.Exit(0);:
if (Status.Equals("Data Got Loaded"))
{
File.WriteAllText(filename2, "0");
aTimer.Stop();
Environment.Exit(0);
}
Do I need to somewhere else so that application should write to the file before getting closed.
basically you can't tap into an event handler when your process is terminated from task manager. see similar post: C# Windows Form Killed By Task Manager...Is There a Way to Run Shutdown Function?
I want to let the user decides if he wants to do a last request at my app when the the Windows is shutting down or logging off. So I am using the "SessionEnding" event.
The following code is triggered but doesn't work. Here is the simplest example:
public App()
{
this.SessionEnding += App_SessionEnding;
}
void App_SessionEnding(object sender, SessionEndingCancelEventArgs e)
{
e.Cancel = true;
}
I am afraid that I've used a boost program before in my computer (like CCleaner), and somehow it deactivated this event. =O
When I add a MessageBox to the event, and I request to log off my computer, then the MessageBox appears, but I don't have time to click somewhere because after 1 second, the system log off. The system seems to not be waiting for my application.
Obs: I am using Windows 7.
I tried a sample of your code without success ... however I put this into my main window that causes the application to close and the MessageBox was displayed indefinitely until I clicked close. Could this help?
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
MessageBox.Show("Blah", "Blah", MessageBoxButton.OK);
base.OnClosing(e);
}
Try overriding the event handler that's already there.
https://wpf.2000things.com/2011/01/25/197-override-application-class-methods-for-standard-events/
public partial class App : Application
{
protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
{
// Always call method in base class, so that the event gets raised.
base.OnSessionEnding(e);
// Place your own SessionEnding logic here
}
}
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
I am having yet another problem with my application for my phone.
My problem is the following:
On one page I have a list of pictures, and a button marked as "favorite." If you are to favorite the particular image of the row, the border of the image changes colors to signify this.
However, whenever I tombstone my application or hit the back button and go back to that very same page, the borders of the images are back to their default color.
Now before you ask, I have no idea if this is an isolated memory problem. I have just begun looking into Isolated storage, and it's difficult for me to grasp at the moment.
Maybe this is a saving state problem?
Either way, I would like to have my application remember what the favorites are whenever a user exits the application or tombstones it, or hits the back button, etc.
Could someone please provide a piece of code in order to help with this?
You need to save your data because the system is basically killing your app. So you need in your app.xaml.cs write code to save and read your data in metods:
// 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)
{
}
// 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)
{
}
// 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)
{
}
// 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)
{
}