Detecting app inactivity in a Windows Phone Application efficiently? - c#

How can I detect if the Application has been idle for let's say 30 seconds?
I know this is possible by using a DispatcherTimer and then restarting it at PhoneApplicationPage.ManipulationCompleted event? But, I am concerned as this will affect the performance of the application.
Are there any better solutions?

You're on the right track. There isn't an explicit "idle" notification (especially not one that fast).
ManipulationCompleted may not always fire for you since other input can prevent the manipulation from starting and a user could do a very long manipulation. I'd reset the timer on any mouse input rather than just on ManipulationCompleted.
Depending on how exact you need your 30 second timer to be I would consider leaving the timer running and setting a flag for the last input. When the timer expires then check if the flag has been set. This way you won't need to continuously reset the timer for every user input.

Related

UWP timer based on system time

I am looking for a way to have an "alarm" type timer that throws an event at a specific system time. Is there a way to do this with UWP apps? I need my application to be able to enter "day mode" at a certain time in the day. Currently, I am using a System.Timers.Timer(), but if the computer goes into sleep mode in the middle of the timer running, it does not count sleep-mode time as part of the timed event. Is there a way to have an alarm type event that is based off of system time as opposed to "timed" time?
Is there a way to have an alarm type event that is based off of system time as opposed to "timed" time?
I have to say that the answer is no. There is no such way that could make an alarm still function in sleep mode. Even the system build-in Alarm app won't work in sleep mode.

Do not exit on clicking back button uwp

I am developing an app in uwp where I dont want to exit the application on back button click(on mobile).Instead of exiting I want to run it in background(it also uses a timer which triggers every 5 second).
Could anybody please help me on this.
Thanks..
A few things to mention:
A UWP app does not exit on pressing back button. It generally goes into a suspended state. It starts running in background only if you have registered a background task.
A background task can be triggered using a time trigger. But the minimum time period between 2 triggers is 15 minutes. So, you might want to look into some other to handle this.
You might want to look at these if you want to understand more clearly
https://blogs.windows.com/buildingapps/2016/04/28/the-lifecycle-of-a-uwp-app/
https://blogs.windows.com/buildingapps/2016/06/07/background-activity-with-the-single-process-model/

Disable a button for t minutes

I want to disable a button for twenty minutes this should remain disabled even if the application is restarted.
I was thinking doing this by reading and storing the time when the button is pressed, then read the system time every minute and when the elapsed time be 20 or more minutes, enable the button.
I think this would allow me to restore the time when the button was pressed if the application is restarted; and then check for the elapsed time.
Do you think this is a good idea?
Any other option?
Since you want this setting to work even if the application restarts, this won't be possible unless you involve an external agent. A few options that you might try involve:
Read current Internet time from http://time.windows.com or nist etc and store it somewhere (registry or local file). Use a timer within your application that keeps fetching latest time from the Internet and compares it to the save value. This post allows you to read current Internet time using both HTTP and TCP port 13.
Use Windows scheduled tasks. Set a bool flag somewhere (file/registry again) and ask the scheduled task to clear the flag after 20 minutes. This post should get you going with creating scheduled tasks.
Create a Windows service that keeps running in the background that you could call to set the flag and the length of time for which this flag should remain set. The service should run an internal Timer (and should not rely on system time) to keep track of "ticks". After the specified time has elapsed, the service should clear he flag.
Here is what I am thinking,
When the application starts, disable or hide the button
Set the time stamp when disabling/hiding and store in a database table or a file.
Now read every minute or every five minutes - whichever is convenient to see if 20 minutes have elapsed.
IF 20 minutes have passed, remove the entry from table or file.
And if the application crashes or restarts before setting the time stamp:
1.While setting the time stamp, make sure that the table or file is empty. If not, enable the timer - the time stamp is there.
Well if I were you, I'd basically disable the button
and then start a timer, each minuite, the program should write the time left to a file as well as the current time ( you can also decrypt this file, so that users can't change it )
Now when the program restarts, it should read this file, and start a timer according to the written time in the fil :)
If you want the timer to work even when the program is closed, then you might check this out (File.GetLastAccessTime)
Basically, you should compare the (current time) that has been written to the fil, with the last access time, and make a function to get the time left :)
This might not be a perfect solution, but it will work fine ( I guess )
Kind Regards :)

How can I get computer status in c#

I want to create something like a client in c#.
But I do not know that how I can learn computer status like sleep mode, off, logged in or logged out.
Also, I need to get the warning if the user haven't used the computer for 10 minutes.
You can find information about currently logged user and how to hook the Locked/Unlocked events in this thread.
There are some other different approaches you might try:
You can use the System.Diagnostics and get the process list via Processes.GetProcesses(). Just keep an eye on the Idle process -- if it runs for more than 50% CPU longer than 10 mins the user seems to be idling too.
You can use Performance Counters to monitor the activity taking place on the computer and make certain decisions.
You can also use the WMI service with similar purposes.
Partial answer:
User activity/inactivity can be monitored using hooks. Start a timer with a 10-minute interval. Whenever you detect a keyboard/mouse message, restart it. If the timer event happens, than you detected 10 minutes of inactivity.

Strategies for keeping a .NET alt-tab replacement responsive after a long period of non-use?

I'm working on a simple alt-tab replacement program. It's implemented in C# as a hidden WPF window that's brought to the foreground by a global keyboard hotkey.
For the most part this works great, and the application window appears immediately after the hotkey is pressed. However, if it has been a long time since the user has activated the window, it can be slow in "spinning up," and take 3-5 seconds to appear. I assume it has something to do with Windows paging app's memory to disk.
Are there any good strategies for keeping the application responsive after long periods of inactivity?
(Since it's open source, you can look at the full code, if it helps.)
Why don't you just use a timer in your code that fires once every minute, and in that timer tick handler, execute some code. This should keep your process "active".

Categories

Resources