-1

I have a children's browser (3 to 11)application, I want a parent to set a timer limit like 30 minutes or anything they choose ( or turn it on or off, preferences probably editable by another form) after which I want it to ask the password before it allows the webbrowser function to work again. Also, there's a problem the child can just restart the app and begin at 30 minutes again. I have currently tried using the timer class/function in C# but I am struggling. Any examples and help would be really appreciated. Below I will add my failed attempt;

 public Booyaa()

        {
            InitializeComponent();


            player.SoundLocation = "BOOYAA.wav";
            player.Play();

            BooyaaTimer.Start();
            BooyaaTimer.Tick += BooyaaTimer_Tick;


        }
private void BooyaaTimer_Tick(object sender, EventArgs e)
        {
            BooyaaTimer.Interval = (6000);              
            BooyaaTimer.Enabled = true;                      
            Time.Text = DateTime.Now.ToString();

            if (BooyaaTimer.Interval <= 0)
            {
                webBrowser1.Hide();
                GetPass pass = new GetPass();
                DialogResult result = pass.ShowDialog();
                if (result == DialogResult.OK)
                {
                    webBrowser1.Show();
                }
            }
        }
  • You need to explain what problem you ran in to, you said "here is my failed attempt" but never said what failed about it. – Scott Chamberlain Mar 01 '17 at 17:58
  • @ScottChamberlain the code has no effect on application – Sharabeel Shah Mar 01 '17 at 18:00
  • Well start debugging. Does `BooyaaTimer_Tick` ever fire? What is the default interval you have set at the start of the program? Why do you subscribe to the event after you start the timer, shouldn't you be subscribing to it before you start? – Scott Chamberlain Mar 01 '17 at 18:02
  • timer interval should be set before starting. interval property will never be 0 in Tick event handler. concerning the second problem, write the last start time to the registry and check on program start. so you could avoid circumventing the time limit by restarting the program (my eight year old can hack the registry, so better add encryption). – Cee McSharpface Mar 01 '17 at 18:03
  • @dlatikay I added the interval and enabled to constructer and still nothing. Also, could explain how you could write the last start time? – Sharabeel Shah Mar 01 '17 at 18:10
  • 1
    looking at the code in your post, I figure there might be a fundamental misunderstanding about how timers work. The `Tick` event handler is called when the timer interval has elapsed. there is no point in waiting for the interval to become zero, inside the handler. see examples of how to use timers [here](http://stackoverflow.com/a/1142893/1132334) btw the requirement in that question looks similar to yours. – Cee McSharpface Mar 01 '17 at 18:24
  • @dlatikay Hooraay! Well, thank you for that! however, I still don't know how to carry the timer information to every time I restart, I didn't understand what you meant before. – Sharabeel Shah Mar 01 '17 at 18:46
  • for example: when the time is over, save the current system time to a registry key. when you start, look if the key is a) missing or b) its value is smaller then current system time plus n minutes. if neither is true, refuse to start. original post is now off topic or no longer reproducable, so could be deleted. – Cee McSharpface Mar 01 '17 at 19:16
  • @dlatikay Could you please give an example? it would be great. – Sharabeel Shah Mar 01 '17 at 19:22

1 Answers1

1

The following is not perfect but I hope it helps.

First, I have a "FormPassword" form for entering the password. This is how you can create it. Create a new form then add a label, a TextBox and two buttons. My TextBox is called textBoxPassword. The two buttons are called buttonOkay and buttonCancel. In the properties for the form, in the "Misc" section, set the AcceptButton to the buttonOkay and the CancelButton to the buttonCancel. Then use the following for the form.

public string Password
{
    get { return textBoxPassword.Text; }
    set { }
}

public FormPassword()
{
    InitializeComponent();
}

private void buttonOkay_Click(object sender, EventArgs e)
{
    Close();
}

Next I am using application settings for storing a "SettingInterval" and a "SettingShutdown". So in the Solution Explorer right-click the project and select Properties. Go to the "Settings" tab and create a "SettingInterval" and "SettingShutdown" settings. Make the type "int" for the SettingInterval and "bool" for SettingShutdown. The SettingShutdown will be used to require a password when the program starts if the program is not stopped correctly. It does not work properly but I am not sure how you want it to work.

The following is the code for the main form. In my form, the button to set the interval and start the timer is called buttonOkay.

public partial class Form1 : Form
{
    DateTime Expires = DateTime.MaxValue;
    bool Warning = false;   // if true, warn when less than 30 seconds

    public Form1()
    {
        InitializeComponent();
        timer1.Tick += Timer1_Tick;
        timer1.Interval = 1000;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        if (!Properties.Settings.Default.SettingShutdown)
        {
            MessageBox.Show("Not properly shut down");
            if (!Authenticate())
                Close();
            Properties.Settings.Default.SettingShutdown = true;
            Properties.Settings.Default.Save();
        }
        textBoxInterval.Text = Properties.Settings.Default.SettingInterval.ToString();
    }

    private bool Authenticate()
    {
        FormPassword f = new FormPassword();
        DialogResult dr = DialogResult.OK;
        for (int i = 0; i < 3 && dr == DialogResult.OK && f.Password != "pass"; ++i)
            dr = f.ShowDialog() ;
        return f.Password == "pass";
    }

    private void Timer1_Tick(object sender, EventArgs e)
    {
        TimeSpan ts = Expires - DateTime.Now;
        Time.Text = ts.ToString("mm\\:ss");
        if (ts.TotalSeconds < 30)
            if (Warning)
            {
                Warning = false;    // this must be before the MessageBox
                MessageBox.Show("You have 30 seconds left!");
            }
        if (ts.TotalSeconds <= 0)
        {
            timer1.Stop();
            MessageBox.Show("Expired");
            Properties.Settings.Default.SettingShutdown = true;
        }
    }

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        Properties.Settings.Default.Save();     // save all the settings
    }

    private void buttonCancel_Click(object sender, EventArgs e)
    {
        Close();
    }

    private void buttonOkay_Click(object sender, EventArgs e)
    {
        int NewInterval = 0;
        if (!Int32.TryParse(textBoxInterval.Text, out NewInterval))
        {
            MessageBox.Show("Not a number");
            return;
        }
        if (!Authenticate())
            return;
        Properties.Settings.Default.SettingInterval = NewInterval;
        Expires = DateTime.Now.AddMinutes(NewInterval);
        Warning = true;
        timer1.Start();
        Properties.Settings.Default.SettingShutdown = false;
        Properties.Settings.Default.Save();
    }
}
Sam Hobbs
  • 2,594
  • 3
  • 21
  • 32
  • Parts of this were very useful, thank you! I adapted that to my code. Now it pops the password dialog up at the end of the timer and remembers this next time it restarts. However, i cannot get it to send a message box through something like if (Interval <= 1 * 60 * 1000) { MessageBox.Show("You have 30 seconds left!"); } Also, i need to implement a text a button which update interval from another form. – Sharabeel Shah Mar 02 '17 at 00:31
  • I have updated the code. I added a "Warning" flag and associated code. As for updating from another form, there are very many articles and answers in many other places. Note that quite often a status bar for messages instead of a MessageBox can be more convenient for the user. – Sam Hobbs Mar 02 '17 at 01:21
  • Also, if you are unfamiliar with Kiosk mode then have a look at [Create a Kiosk Experience](https://msdn.microsoft.com/en-us/windows/hardware/commercialize/customize/enterprise/create-a-kiosk-image) and [Kiosk apps for assigned access Best practices](https://msdn.microsoft.com/en-us/windows/hardware/drivers/partnerapps/create-a-kiosk-app-for-assigned-access). I don't know much about that. – Sam Hobbs Mar 02 '17 at 01:32