Disable a button for t minutes - c#

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 :)

Related

how to handle system wide key events in asp.net application

i am working on an asp.net web application, where tasks are assigned to users, we set standard time to every task, in that standard time period the user has to finish the task, there are two buttons on the page, proceed and save, when a user clicks on proceed button, the time is saved in database as starttime, and when the user clicks on save button, the time is saved in database as endtime. this way we are capturing the time period within which the user is completing the task.
the standard time is set on an average time study basis, not every time the task takes the same amount of time.
often users can complete the task in very less time than the standard time, in this case the users are proceeding the task and even after completing the task, instead of saving it, they lock the system and go for tea breaks and after coming from break, they save the task.
i want to save some information on the web page when they lock the pc even when the browser is minimized.
i tried implementing applet using jintellitype library but its not capturing the key combinations that are used by windows os.
i also tried using Silverlight but there is no such support as in winforms application in Silverlight, i have to create a com component or something that interacts with system32 or some native api. it doesn't seem easy for me, i would like to know if there is such library for Silverlight.
it should be browser independent, i haven't tried ActiveX, but i think it can be done using ActiveX, but i don't want to use ActiveX as it runs only on IE.
i want to know all the possible solutions to achieve this.
thanks in advance.
Why don´t you set a kind of timer-check to know if the last time is too far from the correspondent (and previewed) time to perform the job? If a task may expend, for instance, from 1 to 5 minutes, have 21 minutes is too far.
Why din´t you create a timer to TIMEOUT user? If users know they will be timed-out after some time, probably, they won´t leave to coffre-break during the test (a kind of penalty must be aggregated on this, like start from the initial point if timeout).
Why don´t you automatically save the record after the job finish, instead obly the user to press a button?
Until I know, you can perform SUSPEND mode, but not detect them if started from other apps.

Detecting app inactivity in a Windows Phone Application efficiently?

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.

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.

Countdown Timer c#

Anybody knows how to add a Countdown Timer in c#
Ex:
I have the start time which is 5/2/2011 10:00:00 Am and set the static Duration to 7 Days
So .... the Time left will be 6 Days 12:00 hour till it reach zero. ends in -> 5/9/2011 10:00:00 Am
Note : The Time Left can not be reset and start over again .. and it depends only
on the Start Time
I don't know how to do this .. any help I will be grateful.
You need a permanent storage (on hard disk) like registry, database or XML file because any time span greater than ~24h is not guaranteed in a in-memory code in ASP.NET.
In case you application reset you need to get the start point from somewhere (permanent storage). Usually when there's no request from clients you application would be unloaded without notice (you loose the in-memory data). Also ASP.NET recycles applications based on some configurations in IIS (application pool you are using for your application).
The .NET 4.0 includes the new System.Threading.CountdownEvent class you may find helpful.
A 7 Day timer in a asp.net page?
Perhaps your approach is wrong. Can you use winforms and install the application as a service instead?
I guess if it has to be a webpage, do the timer in JavaScript (perhaps use JQuery), and when its done, do what you want through JQuery. I.E. call your server side method, or whatever.
If you have trouble with the technical aspects of this:
jQuery countdown timer

Show some message every day at 08:00 AM with ASP.NET C#

I want to read my database get values, change my old message with new value and then show this newly message. I want to do this reading job at everyday 08:00. How can I know if clock is 08:00 AM? Need to control every second? or what else?
The key thing to note here is that you don't actually need to do this at 8am - you need to do it when the application is in use at 8am or the first time the application is in use after 8am - if, that is, you want to do this solely within your ASP.NET application.
You need to track when the message was last updated.
In ASP.NET in a suitable application startup event, call the update method - which should check to see if the value needs updating - and then set a timer (its been 5 years since I wrote the code to do this last, options will have changed) for the "next" 8am - when the timer fires, call your update method, set the timer again. So, if the app starts it will update if required, if its running the update will happen.
If you have more access to the server then create a windows service to do the update.
There are several options in between these two - create a page or a web service to trigger the update logic within the ASP.NET application and then use an external timer to open the page/call the web service at the scheduled time. The most appropriate solution depends on your hosting enviroment and the other resources available to you.
you could create a windows service and schedule it for the time you require every day.

Categories

Resources