Time Bomb: Set a Time to Shutdown Your Laptop

Introduction

 
Let me start with a personal question. Are You a Torrent user? If yes, then you must be a bulk downloader. I don't know what your interests are.
 
It may be TV Series, Movies, Game or something else.
 
So, we know that downloading GBs can be very painful if you have an average download speed. For this, we need to keep our Laptop or Desktop on until the download completes. And there are many instances when we have left our Laptop/Desktop open for 24x7.
 
Is it good for our Laptop/Desktop to be turned off after the completion of a download. Isn't it cool!!
 
 
What it actually does
 
It takes a "Time" (Hours: Minutes) as input and makes your Laptop/Desktop turn off when it ticks to that time. In simple words, it is like an alarm except instead of ringing it shuts down your Laptop/Desktop.
 
Background
 
We are not including any third-party library or DLL file. There is only a few .NET classes and predefined Windows Processes.
 
So, let's start building this desktop application.
 
 
Procedure
 
Step 1
 
Before starting the coding part build this UI in a Windows Form.
 
Here, we have used a Picture Control in the top, then two Labels as the Header.
 
Moving down, we have a group Box Control that says "Set Your Time".
 
In that, we have Two Radio Buttons ("Add to Current Time" and "At This Moment"). There we have two Combo Box controls for Hours and Minutes.
 
Below that, we have a Masked Text box that takes a custom time's data (if you want a specific Time).
 
If you are ready with this UI then move to Step 2.
 
Step 2
 
Let's have a look at my Solution Explorer.
 
 
Here I have a single form (in other words Form1) and a BackEnd class (I will show you latter why we need it).
 
In the Resources Directory, we have our images and Icons used in this project.
 
Step 3
 
So, start with a BackEnd class where we have defined a few Property variables.
  1. class BackEnd {  
  2.   
  3.     // Data Fields  
  4.     public static int hour { get;  
  5.         set; }  
  6.     public static int minutes { get;  
  7.         set; }  

Step 4
 
Now, we will code those two Radio Buttons.
 
For the first Radio Button:
 
  1. private void radioButton1_CheckedChanged(object sender, EventArgs e) {  
  2.     // Pre defined : RadioButton 1  
  3.     if (radioButton1.Checked) {  
  4.         hourCombo.Enabled = true;  
  5.         minutesCombo.Enabled = true;  
  6.     } else {  
  7.         hourCombo.Enabled = false;  
  8.         minutesCombo.Enabled = false;  
  9.     }  

Here, we want both the combo boxes to be active when this Radio Button is checked (by default, their Enabled Property is set to False). HourCombo and MinutesCombo are the respective Combo Box's name.
 
And, for the second Radio Button:
 
  1. private void radioButton2_CheckedChanged(object sender, EventArgs e) {  
  2.     // Custome Time  
  3.     if (radioButton2.Checked)  
  4.         customeTime.Enabled = true;  
  5.     else  
  6.         customeTime.Enabled = false;  

Here, all are disabled except the masked Text Box (customeTime).
 
Step 5
 
Now, we will code the Combo Boxes.
 
For the Hour Combo Box, first add the Combo Box's value.
 
Get the Items Property of the Combo Box:
 
 
And, your code will be:
  1. private void hourCombo_SelectedIndexChanged(object sender, EventArgs e) {  
  2.     // When 'Hour' got Selected  
  3.     BackEnd.hour = DateTime.Now.Hour + int.Parse(hourCombo.SelectedItem.ToString());  
  4.     PlantBombButton.Enabled = true;  

Here, we are adding the Selected Hour to the current hour of the computer's time.
 
Note: As when a user wants his Laptop or Desktop to be turned off after two hours.
 
And we do the same for the Minute Combo Box's Property:
 
 
And the code of the Combo box will be:
  1. private void minutesCombo_SelectedIndexChanged(object sender, EventArgs e) {  
  2.     // When 'Minutes' got Selected  
  3.     BackEnd.minutes = DateTime.Now.Minute + int.Parse(minutesCombo.SelectedItem.ToString());  
  4.     PlantBombButton.Enabled = true;  

Step 6
 
For the Masked Text Box, we need to do a little extra.
 
 
Go to the Mask Property of your Control,
 
 
And choose:
 
 
After doing this, we want to code the event.
 
Generally, we code the Click event but this time we will code the Leave event so that the Backend's property (Backend.cs) gets the value just after you leave the Text Box.
 
 
In that event we put this:
  1. string test;  
  2. private void customeTime_Leave(object sender, EventArgs e) {  
  3.   string hr, min;  
  4.   // For Substring, you need a secondary String type variable  
  5.   test = customeTime.Text;  
  6.     
  7.   // leave Event : After The Selection of Text Box  
  8.   hr = test.Substring(0, 2);  
  9.   min = test.Substring(3, 2);  
  10.   if (int.Parse(hr) < 13 && int.Parse(min) < 61) {  
  11.     BackEnd.hour = int.Parse(hr);  
  12.     BackEnd.minutes = int.Parse(min);  
  13.   } else {  
  14.     errorProvider1.SetError(customeTime, "Hour Must be Less than or Equal to 12");  
  15.   }  
  16.   
  17.   //MessageBox.Show(BackEnd.hour+":"+BackEnd.minutes);  
  18.   PlantBombButton.Enabled = true;  
  19.   

Here, we have used an error provider control. You can leave it if you are not comfortable with it.
 
And the rest is explanatory.
 
And, one more thing is that we want the Plant My Bomb Button to be active after this. So:
  1. private void customeTime_MouseClick(object sender, MouseEventArgs e) {  
  2.   // Enable The Plant Bomb text Box  
  3.   PlantBombButton.Enabled = true;  

Step 7
 
We will now code the main button.
  1. private void button4_Click(object sender, EventArgs e) {  
  2.     // This Is Plant Bomb Button  
  3.   
  4.     if (BackEnd.hour.ToString() == "" || BackEnd.minutes.ToString() == "")  
  5.         MessageBox.Show("We Give us The Correct Time :( !!""Invalid Input Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);  
  6.     else {  
  7.         if (MessageBox.Show("Your Bomb Will Activate At : " + BackEnd.hour + " : " + BackEnd.minutes + " GMT .\n Do You Want To Proceed ?""Confirmation Report ", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK) {  
  8.             // If You Pressed Ok  
  9.             // Start Timer  
  10.             tim.Elapsed += new ElapsedEventHandler(tim_Elapsed);  
  11.             tim.Interval = 1000;  
  12.             tim.Enabled = true;  
  13.   
  14.             PlantBombButton.Enabled = false;  
  15.             fuseButton.Enabled = true;  
  16.   
  17.             //Notify Icon Setup  
  18.             bombNotify.ShowBalloonTip(2000);  
  19.             this.Hide();  
  20.         }  
  21.     }  
  22. }  
  23.   
  24. void tim_Elapsed(object sender, ElapsedEventArgs e) {  
  25.     if (DateTime.Now.Hour.ToString() == BackEnd.hour.ToString() && DateTime.Now.Minute.ToString() == BackEnd.minutes.ToString()) {  
  26.         // Shut down Process  
  27.         Process.Start("shutdown""/s /t 0");  
  28.         tim.Stop();  
  29.         this.Close();  
  30.     }  

In the first part of the code it checks if there is no NULL input. And the Print a Message Box depends on it.
 
If everything is fine then it will start a Timer (tim).
 
For every second it calls an event method (tim_elapsed ). And, there we have also defined the interval in milliseconds (and, 1000 is equivalent to 1 second). Finally, enable the Timer and start it.
 
Now, have a look at the Event Handler method.
 
There is an "if" statement that checks whether the current Time matches the specified time.
 
If yes, then execute the Shut down process or leave it.
 
Process. Start() is used to call any other process via C# code. And, we have done the same to call a shutdown process.
 
Note: Keep the arguments in the Start() method as it is.
 
You can change 0 with any other number because it represents TIME (it's better to leave it). Finally, we close that Timmer and Form.
 
Step 8
 
Here we have a Notify Icon control (bombNotify) to ping the last message.
 
 
So, put this Information in its Property window.
 
 
If you click on the Notify icon then it will pop-up the Form windows. So, for that:
  1. private void bombNotify_Click(object sender, EventArgs e) {  
  2.     // MessageBox.Show("Notify Button Clicked ");  
  3.     this.Show();  

For the Disable Button:
 
  1. private void button2_Click(object sender, EventArgs e) {  
  2.     fuseButton.Enabled = false;  
  3.     PlantBombButton.Enabled = true;  
  4.     customeTime.Clear();  
  5.   
  6.     //Timmer  
  7.     tim.Stop();  
  8.     tim.Enabled = false;  
  9.   
  10.     //MessageBox.Show(BackEnd.hour+":"+BackEnd.minutes+" And Your Current"+DateTime.Now.Hour+":"+DateTime.Now.Minute);  

I hope all sections have been covered (if not then try to find it in the Solution File).
 
So, let's move to debugging.
 
Output
 
 
Set the Time to 04:30.
 
Then, click on "Plant My Bomb".
 
 
And, they are asking for confirmation.
 
And when I click on "OK", then:
 
 
Then, it shows a Popup in the System tray.
 
And, if you want the Bomb to be disabled then for that, you need to click on the Bomb Icon. Then, it comes up with this.
 
To disable, click on the Red Button.
 
 

Conclusion

 
If you encounter any issue with it then message me or try to get it with the enclosed solution file.


Similar Articles