Scott's Oasys
Scott's Oasys

Reputation: 154

c# Shutdown a system after holding yes for 5 seconds

I was wondering if anyone knows how to use a dialog box to create a hold down button event. Here is the scenerio:

a user would like to shutdown their system, but because it is critical that they confirm, that user must hold the button for 5 seconds before the action can be done.

I am trying to do it in a yes no scenario ie.

To confirm shutdown please hold "Yes" for 5 seconds.

Anyone done this before able to offer a little help/insight?

Upvotes: 0

Views: 1575

Answers (9)

Inisheer
Inisheer

Reputation: 20794

Try using a button's Mouse_Down & Mouse_Up event, and a timer (this assumes you're using WinForms).

    private void button1_MouseDown(object sender, MouseEventArgs e)
    {
        if (this.timer1.Enabled == false)
        {
            this.timer1.Interval = 5000;
            this.timer1.Enabled = true;
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        this.timer1.Enabled = false;
        MessageBox.Show("Shutdown!");
    }

    private void button1_MouseUp(object sender, MouseEventArgs e)
    {
        timer1.Enabled = false;
    }

Upvotes: 5

Matt H
Matt H

Reputation: 7389

You can set up a timer on the MouseDown event, and if the mouse capture changes (check the MouseCaptureChanged event) to false before the timer event fires, cancel the timer.

Upvotes: 0

Adam Barney
Adam Barney

Reputation: 2387

private DateTime mouseDownTime;

private void Button_MouseDown(object sender, MouseButtonEventArgs e)
{
    mouseDownTime = DateTime.Now;
}

private void Button_MouseUp(object sender, MouseButtonEventArgs e)
{
    if (mouseDownTime.AddSeconds(5) < DateTime.Now)
        MessageBox.Show("You held it for 5 seconds!");
}

Upvotes: 0

George Johnston
George Johnston

Reputation: 32258

You could do this any number of ways. The first that comes to my mind would be to spin off a thread that waits 5 seconds and is simply aborted if the user's mouse comes back up.

    Thread shutdown;
    private void button1_MouseDown(object sender, MouseEventArgs e)
    {
        shutdown = new Thread(()=>ShutDown());
        shutdown.Start();
    }

    private void ShutDown()
    {
        Thread.Sleep(5000);
        Console.Write("5 seconds has elapsed");
        // Do something.
    }

    private void button1_MouseUp(object sender, MouseEventArgs e)
    {
        if (shutdown != null)
        {
            shutdown.Abort();
            shutdown = null;
        }
    }

Low overhead and you're not adding additional supporting controls for something this simple.

Upvotes: 1

Nils Luxton
Nils Luxton

Reputation: 766

You could capture the button press on 'mousedown', and start a 5-second timer. Once the timer completes, shutdown is initiated. If a 'mouseup' event happens, it could stop and reset the timer.

Upvotes: 4

Evan Mulawski
Evan Mulawski

Reputation: 55334

When the user first clicks YES, start a timer that repeatedly checks if the mouse location is inside of the button. After 5 seconds has elapsed, proceed with the shutdown. If the user moves the mouse out of the button, stop the timer.

Upvotes: 0

GendoIkari
GendoIkari

Reputation: 11914

You can use the Form.MouseDown events do detect that the user has pressed the mouse button. In the event handler, check to see if cursor is over the button or not (the event is passed in the coordinates of the cursor). You can then enable a timer which will tick in 5 seconds, and perform the shutdown when the timer ticks.

Upvotes: 0

Pedery
Pedery

Reputation: 3636

Sure, handle BOTH the mousedown event and the mouseup event. Start a timer on the mousedown and see how long it has run on the mouseup. Done!

Upvotes: 2

LostInTheCode
LostInTheCode

Reputation: 1744

Why bother when you can just use getAsyncKeyState()? Tell them to hold down 'y' for 5 seconds. You can find a reference here: http://www.pinvoke.net/default.aspx/user32.getasynckeystate

Or you can do it your way and start a timer on MouseDown, then on MouseUp, end the timer and then see if it's more or less than 5 seconds. http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousedown%28VS.71%29.aspx

Upvotes: 0

Related Questions