Click on the timer and add the following code.
C#
private void tmrReboot_Tick(object sender,
System.EventArgs e)
{
lblCurrentTime.Text = "The time is " +
DateTime.Now.ToShortTimeString();
if
(DateTime.Parse(dateTimePicker.Text).ToShortTimeString() ==
DateTime.Now.ToShortTimeString() && chkReboot.Checked)
{
reboot();
}
}
VB.NET
Private Sub tmrReboot_Tick(ByVal sender As
System.Object, ByVal e As _ System.EventArgs) Handles tmrReboot.Tick
lblCurrentTime.Text = "The time is " +
DateTime.Now().ToShortTimeString()
If
DateTime.Parse(DateTimePicker.Text).ToShortTimeString() = _
DateTime.Now().ToShortTimeString() And chkReboot.Checked Then
reboot()
End If
End Sub
What this code does is that it displays the current time on
screen, in an easy to read format. If the current time matches the time as set
with the DateTimePicker
and the reboot check box has been checked then the reboot function is called.
The reboot() function needs to be implemented. This
firstly acquires privileges to reboot the computer, and then calls a windows
API function to perform the reboot. Parameters are passed to the function which
forces a unattended shutdown, where whenever possible any requests for user intervention
are ignored.
C#
public void reboot()
{
AdjustToken();
ExitWindowsEx(EWX_SHUTDOWN
| EWX_FORCE | EWX_REBOOT, 0xFFFF);
}
VB.NET
Public Sub reboot()
AdjustToken()
ExitWindowsEx(EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT, &HFFFF)
End Sub
A reboot requires special privileges in order to be called
programmatically. This goes some way to make it more difficult for rogue
processes, or viruses to potentially render a computer useless by constantly
rebooting it. The function AdjustToken is used to grant these privileges to the
application.
There is a particular style to Windows API programming. It
doesn’t have the same neat object oriented architecture as the .NET framework,
instead when a resource is requested an integer or handle is returned rather than
an object representing the resource. This handle can then be passed to related
functions, which then can make alterations to the instance of the resource
retrieved in the previous call.
To request extra privileges for a process, the fist task is
to get the current process handle using GetCurrentProcess(). With
this handle, the process token can be retrieved. The process token contains
information on what actions the process is permitted to perform. To request
extended permissions to the process token, and therefore the process itself, a
reference to the specific permission is requested, then the token is adjusted
with a call to AdjustTokenPriviliges().
C#
private void AdjustToken()
{
TOKEN_PRIVILEGES tkp;
IntPtr
hproc = GetCurrentProcess();
IntPtr
htok = IntPtr.Zero;
OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
ref htok );
tkp.Count = 1;
tkp.Luid
= 0;
tkp.Attr
= SE_PRIVILEGE_ENABLED;
LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tkp.Luid );
AdjustTokenPrivileges( htok, false, ref tkp,
0,IntPtr.Zero,IntPtr.Zero);
}
Page 1
Page 3