Automatic System Locking Using Timer

Introduction

In this article, I am showing how to lock the system automatically using a timer.

Generally, we lock our system using the key combination "Ctrl + Alt +Del" and then choose "Lock the Computer". What if we want our system locked automatically after a certain time? Let us see how to lock our system automatically using the timer. A sample project is also attached to this article. A screenshot of the program is shown below. In this app, the user selects the time unit in the combo box, enters the time, and clicks the "Go...Lock the system," and the app locks the system after a given specified time.

Auto-System-Locker.jpg

When the user selects any time unit in the Combo Box, the following "SelectedIndex" event handler method of the Combo Box is called. In this method, we convert the selected time units into milliseconds.

private void cmboBox_Unit_SelectedIndexChanged(object sender, EventArgs e)
{
    string _timeUnit = cmboBox_Unit.SelectedItem.ToString();

    switch (_timeUnit)
    {
        case "second(s)":
            // Initialized with 1 Second (1000 Milliseconds)
            multiplicationFactor = 1000;
            break;
        case "Minute(s)":
            // Initialized with 1 Minute (60 * 1000 Milliseconds)
            multiplicationFactor = 60 * 1000;
            break;
        case "Hour(s)":
            // Initialized with 1 Hour (60 * 60 * 1000 Milliseconds)
            multiplicationFactor = 60 * 60 * 1000;
            break;
        case "Day(s)":
            // Initialized with 1 Day (24 * 60 * 60 * 1000 Milliseconds)
            multiplicationFactor = 24 * 60 * 60 * 1000;
            break;
    }
}

After setting the time, the user clicks a button to lock the system. In the following "Click" event handler of the button, we first check the validity of the entered number using int. TryParse.

If the number is valid, we instantiate the "Timer" object. Now we set the timer interval (in milliseconds) and create an event handler "tmr_Tick" for the "Tick" event, which means that after a certain number of ticks (in other words, an interval), the timer will raise this event. After setting up the Timer's interval and Tick event handler, we just start the Timer and wait for the event to be fired on a specified interval.

Timer _tmr = new Timer();
_tmr.Interval = interval;
_tmr.Tick += new EventHandler(tmr_Tick);
this.UseWaitCursor = true;
_tmr.Start();

Here is the full code of the "Click" event handler.

private void btn_Lock_Click(object sender, EventArgs e)
{
    int _resultNum = -1;
    if (int.TryParse(txtBx_TimerInterval.Text, out _resultNum) && _resultNum > 0)
    {
        interval = _resultNum * multiplicationFactor;
        Timer _tmr = new Timer();
        _tmr.Interval = interval;
        _tmr.Tick += new EventHandler(tmr_Tick);
        this.UseWaitCursor = true;
        _tmr.Start();
    }
    else
    {
        MessageBox.Show("Please enter any positive non-zero number.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        txtBx_TimerInterval.Focus();
    }
}

In the Timer's Tick event handler, we start a process to lock the Windows system. We lock the system using a built-in Windows process, "Rundll32.exe," by ing the two arguments "User32.dll" and "LockWorkStation."

Example. Here is the code.

void tmr_Tick(object sender, EventArgs e)
{
    string _filename = "Rundll32.exe";
    string _arguments = "User32.dll, LockWorkStation";
    ProcessStartInfo _startinfo = new ProcessStartInfo(_filename, _arguments);
    Process.Start(_startinfo); 
    this.Close();
}

I hope this simple article will help you to set Automatic system locking. If you have any issues or concerns related to this article, I will be happy to discuss them.

Thanks


Similar Articles