简体   繁体   中英

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?

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;
    }

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.

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!

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.

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

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.

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.

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!");
}

您可以在MouseDown事件上设置一个计时器,如果在触发该计时器事件之前鼠标捕获的变化(检查MouseCaptureChanged事件)为false,则取消该计时器。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM