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
Related
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.
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 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 have a C# GUI app that shows a message using MessageBox.Show(Message);, however if the user fails to click on this, and then requests shutting down the PC, it blocks the shutdown.
How do I prevent my open dialog box from blocking the shutdown?
I'm assuming you're using WinForms since you didn't mention WPF. You can't use a MessageBox if you want to control closing behavior. You'll have to build your own screen to act as a message box and use the ShowDialog method to display it. Your screen can handle the FormClosing event to detect when Windows is shutting down:
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.WindowsShutDown)
{
//...
}
}
So you'll want to allow the screen to close in this case and perhaps take other action for other types of close signals. To prevent the screen from closing, set the Cancel flag on the FormClosingEventArgs parameter to true;
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