0

I am working on antivirus program and on real-time protection panel I want checkbox when for example "Malware protection" checkbox is unchecked to make it not enable for like 15 minutes and after that time it is enabled again so it prevents spam. If somebody can help me it would be great

I tried with Thread.Sleep() but it stops whole application, and I tried with timer but I think I did it wrong.

This is code for timer

private void checkBox1_CheckStateChanged(object sender, EventArgs e)
{
    if (this.checkBox1.Checked)
    {
        this.checkBox1.Text = "On";
        // these two pictureboxes are for "You are (not) protected"
        // picture
        picturebox1.Show();
        pictureBox5.Hide();
        timer1.Stop();
    }
    else
    {
        this.checkBox1.Text = "Off";
        // this is the problem
        timer1.Start();
        this.checkBox1.Enabled = true;
        pictureBox1.Hide();
        pictureBox5.Show();
    }
}

private void timer1_Tick(object sender, EventArgs e)
{
    this.checkBox1.Enabled = false;
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
Vepth
  • 665
  • 4
  • 20
  • Have you set `timer1.Enabled = true;`? – stuartd Jan 16 '19 at 16:44
  • take a look at this: https://stackoverflow.com/questions/1416803/system-timers-timer-vs-system-threading-timer What you need is to make it thread safe. The timer should be on a different Thread so the interface doest block while executing – Kim Lage Jan 16 '19 at 16:47
  • another source for this: https://stackoverflow.com/questions/11689866/how-to-achieve-thread-safety-with-a-timed-windows-forms-update – Kim Lage Jan 16 '19 at 16:48

4 Answers4

2

Short Answer

From the code you posted, it really only appears that you need to change the code to disable the checkbox in the CheckChanged event and enable it in the timer1_Tick event (and also Stop the timer in the Tick event).

Full Answer

Winforms has a Timer control that you can use for this. After you drop a Timer onto the designer, set the Interval property to the number of milliseconds you want to wait before enabling the checkbox (1 second is 1000 milliseconds, so 15 minutes is 15min * 60sec/min * 1000ms/sec, or 900,000 ms). Then double-click it to create the Tick event handler (or add one in your Form_Load event as I've done below).

Next, in the CheckChanged event, if the checkbox is not checked, disable the checkbox and start the timer.

Then, in the Tick event, simply enable the checkbox (remember, this event is triggered after Interval milliseconds have passed) and stop the timer.

For example:

private void Form1_Load(object sender, EventArgs e)
{
    // These could also be done in through designer & property window instead
    timer1.Tick += timer1_Tick; // Hook up the Tick event
    timer1.Interval = (int) TimeSpan.FromMinutes(15).TotalMilliseconds; // Set the Interval
}

private void timer1_Tick(object sender, EventArgs e)
{
    // When the Interval amount of time has elapsed, enable the checkbox and stop the timer
    checkBox1.Enabled = true;
    timer1.Stop();
}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    if (!checkBox1.Checked)
    {
        // When the checkbox is unchecked, disable it and start the timer
        checkBox1.Enabled = false;
        timer1.Start();
    }
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • Problem was that I though that interval means for how much time timer is doing something. For example timer1.Start(); this.checkBox1.Enabled = true; I though that because in timer1_tick I set checkbox to be disabled for 1000 milliseconds but it's not. Thanks for help! – Vepth Jan 16 '19 at 17:30
0

This can be done without using Timer explicitly. Instead use asynchronous Task.Delay, which will simplify the code and make it easy to understand actual/domain intentions.

// Create extension method for better readability
public class ControlExtensions
{
    public static Task DisableForSeconds(int seconds)
    {
        control.Enabled = false;
        await Task.Delay(seconds * 1000);
        control.Enabled = true;
    }
}

private void checkBox1_CheckStateChanged(object sender, EventArgs e)
{
    var checkbox = (CheckBox)sender;
    if (checkbox.Checked)
    {
        checkbox.Text = "On";
        picturebox1.Show();
        pictureBox5.Hide();
    }
    else
    {
        checkbox.Text = "Off";
        checkbox.DisableForSeconds(15 * 60);
        pictureBox1.Hide();
        pictureBox5.Show();
    }
}
Fabio
  • 31,528
  • 4
  • 33
  • 72
-1

You should use Timer.SynchronizationObject

OMGtechy
  • 7,935
  • 8
  • 48
  • 83
  • 1
    Hello and welcome to SO! Thanks for trying to help someone out, however your answer is currently without explanation. Could you please explain (a) how you think it should be used and (b) how it helps answer the question? To make it even better, you could give some pros and cons for it too. – OMGtechy Jan 16 '19 at 17:12
  • Generally, recommendations for a function, documentation, tool, or library [should be accompanied by usage notes, a specific explanation of how the resource is applicable to the problem, and some sample code](//meta.stackoverflow.com/a/251605/584192). – Samuel Liew Jan 16 '19 at 22:57
-1

You could diseable and enable it with task.Delay(). ContinueWith(). This creates a new thread that fires after the delay is done. You need to make it thread safe, winforms isnt thread safe on its own

Maxime C.
  • 37
  • 11