0

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?

rena
  • 1,223
  • 3
  • 17
  • 30
  • What if the user changes his system time? Does the system have access to an internet connection to get network time from say nist? Depends how secure you want this to be. – Ryan Feb 18 '16 at 03:02
  • The user cannot change the time of his workstation, its disabled by a gpo. And yes the system has Internet connection so I can get the time from a ntp server. – rena Feb 18 '16 at 03:12
  • 1
    What is the purpose for doing this? Is the user good guy or bad guy? If you can limit the access of the computer to the user, storing elapsed time in PC is okay. If no and want to be secure enough, then, the disable begin time of a server PC should stored in server, then the application should try to ask server to enable the button again periodically. – daniel Feb 18 '16 at 03:12
  • Do you have a server/database? – CharithJ Feb 18 '16 at 03:13
  • As the pc has gpo, then make sure you store the time in secure way which also prevent user to change by gpo. – daniel Feb 18 '16 at 03:16

3 Answers3

3

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:

  1. 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.

  2. 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.

  3. 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.

Community
  • 1
  • 1
dotNET
  • 33,414
  • 24
  • 162
  • 251
1

Here is what I am thinking,

  1. When the application starts, disable or hide the button
  2. Set the time stamp when disabling/hiding and store in a database table or a file.
  3. Now read every minute or every five minutes - whichever is convenient to see if 20 minutes have elapsed.
  4. 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.

mojorisinify
  • 377
  • 5
  • 22
-1

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