Alarm without message dialog - c#

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.

Related

BackRequested event handler event.Handled is ignored

So, as per the docs I attach a handler when I navigate to a page:
protected override void OnNavigatedTo(NavigationEventArgs e) {
/* Attach back listener to handle the back press */
SystemNavigationManager.GetForCurrentView().BackRequested += NavigationPage_BackRequested;
...
}
And I detach it when leaving:
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) {
SystemNavigationManager.GetForCurrentView().BackRequested -= NavigationPage_BackRequested;
e.Cancel = false;
}
And I mark the event as handled to prevent the system from handling it:
private void NavigationPage_BackRequested(object sender, BackRequestedEventArgs e) {
e.Handled = true;
}
But the system still handles the back click and I get navigated away. Why?
Your code shown above is working fine (tested on my machine), but the big question is: do you try to cancel a system provided navigation event or a Frame.GoBack() event implemented by the software button of your application?
SystemNavigationManager: Provides a way for an app to respond to system provided back-navigation events.
If you look at the backwards navigation documentation, only certain back buttons (hardware/software) are system provided events, e.g. the software button at the bottom in Tablet mode.
However quite a few project templates (in Visual Studio or from MVVM libraries) also provide a software back button (quite often at the top left) and wire this event to the Frame.GoBack() method. This is NOT a system provided event and can't be cancelled this way. Reasoning: You (or the framework used) is calling the GoBack() explicitly, so why would it have to be cancelled in this scenario.

How do I prevent any button click events from queuing until the event handle is finished with the first call

I want to prevent a button click from queuing. In testing I have a Form, a Button and in the Code-Behind I have the event handler:
private void button1_Click(object sender, EventArgs e)
{
if (_codeRunning)
return;
_codeRunning = true;
//Application.DoEvents();
//button1.Enabled = false;
_click ++;
Debug.WriteLine("Click Number: " + _click);
Task.Delay(5000).Wait();
//button1.Enabled = true;
_codeRunning = false;
}
When I run debug and click the button twice or three or four times rapidly, Debug Output shows each click about five seconds after the last one. What I would like it to show is a single Click and drop the rest until first Event is complete.
I have also tried to disable the button, as well as temporarily remove the Handler from the Button_click event. It is all the same results.
There are various amounts of trouble you'll get into when you hang-up the UI thread like this. This is certainly one of them, nothing pleasant happens when the user wildly bangs on the button to try to get something noticeable to happen. And sure, those clicks won't get lost, they stay stored in the message queue. To activate your Click event handler again when your event handler stops running.
Pretty important to learn how to use the BackgroundWorker or Task classes to avoid this kind of trouble. Just setting the button's Enabled property is then enough to solve this problem.
Purging the mouse clicks from the message queue is technically possible. But ugly to do, it requires pinvoke. I'll hesitantly post the alternative, don't assume that this is in general a good strategy. You'll need to read this post to have some insight into why DoEvents() is a dangerous method.
private void button1_Click(object sender, EventArgs e) {
button1.Enabled = false;
button1.Update();
'' long running code
''...
Application.DoEvents();
if (!button1.IsDisposed) button1.Enabled = true;
}
The Update() call ensures that the user gets the feedback he needs to know that banging the button repeatedly isn't going to do anything useful. The DoEvents() call will dispatch all the queued-up mouse clicks, nothing happens with them since the button is still disabled. The IsDisposed test is essential to solve the problem with DoEvents(), it ensures your program won't crash when the user clicked the window's Close button while the code was running.
Use the HourGlass class in this post to provide more feedback.
I had a button that on click event was going to run a method. Same issue happent and when the user clicked multiple times the method was triggered multiple times. So I made a boolean and changed it value when the method started.
private bool IsTaskRunning = false;
private void MyMethod()
{
if ( IsTaskRunning==false )
{
IsTaskRunning=true;
// My heavy duty code that takes a long time
IsTaskRunning=false; // When method is finished
}
}
So now the method runs only if it's done the last time.

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.

Session Ending event not working

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
}
}

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