Shutdown Computer Using C#.net(Cessation)


Cessation
Cessation is the project which is able to shut down your computer or reboot and perform many tasks like logoff, switch user, sleep, hibernate just one click but don’t forget to set the time for task. I use this project when I want to shut down my computer when some downloading finished. I this project is developed in c#.net 2010.
Download
You can get this project by open this link http://www.4shared.com/file/XqTzCYuH/cessation.html
Description
When developing this project I use Radio button, panels, timer, button, tools tip, notification, Doman up down components.Radio button: radio button is use to select the task it may be shut down etc…Timer is use to track the time and trigger the code of your selected task.One reset button is use to reset the application fields and abort the task.Tool tip is use to show the tip on particular components.Notification in use to send my APPs to notification area.Domain up down component is to select the hour minutes and seconds and AM/PM.

Note: Don’t forget to include user32.dll file, write this code after the class.

<br />
[DllImport(&quot;user32.dll&quot;)]<br />
 public static extern void LockWorkStation();<br />
 [DllImport(&quot;user32.dll&quot;)]<br />
 public static extern int ExitWindowsEx(int uFlags, int dwReason);<br />

Screen shot

Main form

Popup window

About Me

here the following code for shutdown,reboot, log-off,lock,switch user,hibernate,sleep

<br />
//Code for shutdown:<br />
System.Diagnostics.Process.Start(&quot;shutdown&quot;, &quot;/s /f /t 0&quot;);<br />
//Code for reboot<br />
System.Diagnostics.Process.Start(&quot;shutdown&quot;, &quot;/r /f /t 0&quot;);<br />
//Code for sleep:<br />
Application.SetSuspendState(PowerState.Suspend, true, true);<br />
//Code for logoff:<br />
ExitWindowsEx(0, 0);<br />
//Code for lock the computer:<br />
LockWorkStation();<br />
//Code for hibernate<br />
Application.SetSuspendState(PowerState.Hibernate, true, true);<br />

Display Current System Time

For displaying the current system time the following code is responsible,Timer1 code is trigger every one second

<br />
private void timer1_Tick(object sender, EventArgs e)<br />
 {<br />
 lbl_time.Text = Convert.ToString(DateTime.Now.ToLongTimeString());<br />
 }<br />

Displaying the message:

This code is responsible for display the default message on the status area.

<br />
private void tm_msg_Tick(object sender, EventArgs e)<br />
 {<br />
 lbl_msg.ForeColor = Color.Black;<br />
 lbl_msg.Text = &quot;Set Time and opration for performe the Task.&quot;;<br />
 tm_msg.Enabled = false;<br />
 }<br />

Check all the input is properly given or not

For checking all the input is proper given or not . Time and task is set or not if it is set then display the message like “your computer will be shut down at 12:00:00 pm” and display the popup window, and show notify tool tip.Trace the time and complete the peration This code is trace the time if the given time and system time is match then it execute the given task code.

<br />
private void tm_setgo_Tick(object sender, EventArgs e)<br />
{<br />
string tm = (ud_hh.Text + &quot;:&quot; + ud_mm.Text + &quot;:&quot; + ud_ss.Text + &quot; &quot; + ud_pa.Text);<br />
if (flag == true &amp;amp;&amp;amp; flag2 == true &amp;amp;&amp;amp; tm == Convert.ToString(DateTime.Now.ToLongTimeString()))<br />
{<br />
tm_setgo.Enabled = false;<br />
if (rd_hiber.Checked == true)<br />
{<br />
frm_cess.Show();<br />
Application.SetSuspendState(PowerState.Hibernate, true, true);<br />
msg = &quot;Hibernate&quot;;<br />
}<br />
if (rd_lock.Checked == true)<br />
{<br />
cessation_task fm_cess=new cessation_task();<br />
fm_cess.Show();<br />
LockWorkStation();<br />
msg = &quot;Lock&quot;;<br />
}<br />
if (rd_logoff.Checked == true)<br />
{<br />
cessation_task fm_cess = new cessation_task();<br />
fm_cess.Show();<br />
ExitWindowsEx(0, 0);<br />
msg = &quot;Logoff&quot;;<br />
}<br />
if (rd_res.Checked == true)<br />
{<br />
cessation_task fm_cess = new cessation_task();<br />
frm_cess.Show();<br />
System.Diagnostics.Process.Start(&quot;shutdown&quot;, &quot;/r /f /t 0&quot;);<br />
msg = &quot;Reboot&quot;;<br />
}<br />
if (rd_sleep.Checked == true)<br />
{<br />
cessation_task fm_cess = new cessation_task();<br />
fm_cess.Show();<br />
Application.SetSuspendState(PowerState.Suspend, true, true);<br />
msg = &quot;Sleep&quot;;<br />
}<br />
if (rd_sh.Checked == true)<br />
{<br />
cessation_task fm_cess = new cessation_task();<br />
fm_cess.Show();<br />
System.Diagnostics.Process.Start(&quot;shutdown&quot;, &quot;/s /f /t 0&quot;);<br />
msg = &quot;Shutdown&quot;;<br />
}<br />
}<br />
}<br />

Send for to notify area and display the pop up window

To hide the form and display the notification and popup window the following code is responsible

For tracing the window state define one function definition in form1.designer.cs File inside the private void InitializeComponent() Function.

<br />
private void mn_cessation_Resize(object sender, EventArgs e)<br />
{<br />
if (this.WindowState == FormWindowState.Minimized)<br />
{<br />
this.Hide();<br />
if(msg!=&quot;&quot;)<br />
frm_cess.Show();<br />
}<br />
}<br />

Main form(form1.cs ) source code

<br />
using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
using System.Runtime.InteropServices;<br />
using System.Management;<br />
namespace cessation<br />
{<br />
 public partial class mn_cessation : Form<br />
{<br />
 // Importing Windows API library<br />
 [DllImport(&quot;user32.dll&quot;)]<br />
 public static extern void LockWorkStation();<br />
 [DllImport(&quot;user32.dll&quot;)]<br />
 public static extern int ExitWindowsEx(int uFlags, int dwReason);<br />
 bool flag=false;<br />
 bool flag2=false;<br />
 string msg;<br />
 int flg=0;<br />
 cessation_task frm_cess = new cessation_task();<br />
 public mn_cessation()<br />
 {<br />
 InitializeComponent();<br />
 }<br />
 private void mn_cessation_Load(object sender, EventArgs e)<br />
 {<br />
 msg = &quot;&quot;;<br />
 tm_time.Enabled = true;<br />
 }<br />
private void mn_cessation_Resize(object sender, EventArgs e)<br />
{<br />
 if (this.WindowState == FormWindowState.Minimized)<br />
 {<br />
 this.Hide();<br />
 if(msg!=&quot;&quot;)<br />
 frm_cess.Show();<br />
 }<br />
 }<br />
 private void timer1_Tick(object sender, EventArgs e)<br />
 {<br />
 lbl_time.Text = Convert.ToString(DateTime.Now.ToLongTimeString());<br />
 }<br />
 private void btn_rst_Click(object sender, EventArgs e)<br />
 {<br />
 btn_rst.Size = new Size(30, 27);&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;<br />
 reset();<br />
 btn_rst.Size = new Size(29, 26);<br />
 }<br />
 private void btn_rst_MouseLeave(object sender, EventArgs e)<br />
 {<br />
 btn_rst.Size = new Size(29, 26);<br />
 }<br />
 private void btn_rst_MouseHover(object sender, EventArgs e)<br />
 {<br />
 btn_rst.Size = new Size(30, 27);<br />
 }<br />
 public void reset()<br />
 {<br />
 msg = &quot;&quot;;<br />
 ud_hh.Text = &quot;&quot;;<br />
 ud_mm.Text = &quot;&quot;;<br />
 ud_ss.Text = &quot;&quot;;<br />
 ud_pa.Text = &quot;&quot;;<br />
 rd_hiber.Checked = false;<br />
 rd_lock.Checked = false;<br />
 rd_logoff.Checked = false;<br />
 rd_res.Checked = false;<br />
 rd_sh.Checked = false;<br />
 rd_sleep.Checked = false;<br />
 tm_msg.Enabled = true;<br />
 lbl_msg.ForeColor = Color.Blue;<br />
 lbl_msg.Text = &quot;Time and Operation will reset and task will be cancelled.&quot;;<br />
 flag2 = false;<br />
 flag = false;<br />
 }<br />
 private void rd_sh_CheckedChanged(object sender, EventArgs e)<br />
 {<br />
 if (rd_sh.Checked == true)<br />
 {<br />
 pic_oper.Visible = false;<br />
 pic_opr.Visible = true;<br />
 flag2 = true;<br />
 tm_setgo.Enabled = true;<br />
 msg = &quot;Shutdown&quot;;<br />
 }<br />
 if (rd_sh.Checked == false)<br />
 {<br />
 pic_oper.Visible = true;<br />
 pic_opr.Visible = false;<br />
 }<br />
 }<br />
 private void tm_msg_Tick(object sender, EventArgs e)<br />
 {<br />
 lbl_msg.ForeColor = Color.Black;<br />
 lbl_msg.Text = &quot;Set Time and opration for performe the Task.&quot;;<br />
 tm_msg.Enabled = false;<br />
 }<br />
 private void tm_check_Tick(object sender, EventArgs e)<br />
 {<br />
 if (ud_hh.Text == &quot;&quot; &amp;amp;&amp;amp; ud_mm.Text == &quot;&quot; &amp;amp;&amp;amp; ud_ss.Text == &quot;&quot; &amp;amp;&amp;amp; ud_pa.Text == &quot;&quot;)<br />
 {<br />
 pic_tmr.Visible = false;<br />
 pic_tmer.Visible = true;<br />
 flag = false;<br />
 }<br />
 if (ud_hh.Text != &quot;&quot; &amp;amp;&amp;amp; ud_mm.Text != &quot;&quot; &amp;amp;&amp;amp; ud_ss.Text != &quot;&quot; &amp;amp;&amp;amp; ud_pa.Text != &quot;&quot;)<br />
 {<br />
 DateTime d = DateTime.Now;<br />
 pic_tmr.Visible = true;<br />
 pic_tmer.Visible = false;<br />
 flag = true;<br />
 if (msg != &quot;&quot;)<br />
 {<br />
 int hh, mm, ss;<br />
 string tm;<br />
 tm = (ud_hh.Text + &quot;:&quot; + ud_mm.Text + &quot;:&quot; + ud_ss.Text + &quot; &quot; + ud_pa.Text);<br />
 lbl_msg.Text = &quot;Your computer will &quot; + msg + &quot; at &quot; + tm;<br />
 nty_cess.Text = &quot;Your computer will &quot; + msg + &quot; at &quot; + tm;<br />
 frm_cess.msg(lbl_msg.Text);<br />
 if (flg == 0)<br />
 {<br />
 nty_cess.ShowBalloonTip(1000, &quot;cessation&quot;, &quot;Your computer will &quot; + msg + &quot; at &quot; + tm , ToolTipIcon.Info);<br />
 flg++;<br />
 }<br />
 }<br />
 }<br />
 tm_check.Enabled = true;<br />
 }<br />
private void rd_hiber_CheckedChanged(object sender, EventArgs e)<br />
 {<br />
 if (rd_hiber.Checked == true)<br />
 {<br />
 pic_oper.Visible = false;<br />
 pic_opr.Visible = true;<br />
 flag2 = true;<br />
 msg = &quot;Hibernate&quot;;<br />
 tm_setgo.Enabled = true;<br />
 }<br />
 if (rd_hiber.Checked == false)<br />
 {<br />
 pic_oper.Visible = true;<br />
 pic_opr.Visible = false;<br />
 }<br />
 }<br />
 private void rd_lock_CheckedChanged(object sender, EventArgs e)<br />
 {<br />
 if (rd_lock.Checked == true)<br />
 {<br />
 pic_oper.Visible = false;<br />
 pic_opr.Visible = true;<br />
 flag2 = true;<br />
 msg = &quot;Lock&quot;;<br />
 tm_setgo.Enabled = true;<br />
 }<br />
 if (rd_lock.Checked == false)<br />
 {<br />
 pic_oper.Visible = true;<br />
 pic_opr.Visible = false;<br />
 }<br />
 }<br />
 private void rd_logoff_CheckedChanged(object sender, EventArgs e)<br />
 {<br />
 if (rd_logoff.Checked == true)<br />
 {<br />
 pic_oper.Visible = false;<br />
 pic_opr.Visible = true;<br />
 flag2 = true;<br />
 msg = &quot;Logoff&quot;;<br />
 tm_setgo.Enabled = true;<br />
 }<br />
 if (rd_logoff.Checked == false)<br />
 {<br />
 pic_oper.Visible = true;<br />
 pic_opr.Visible = false;<br />
 }<br />
 }<br />
 private void rd_res_CheckedChanged(object sender, EventArgs e)<br />
 {<br />
 if (rd_res.Checked == true)<br />
 {<br />
 pic_oper.Visible = false;<br />
 pic_opr.Visible = true;<br />
 flag2 = true;<br />
 msg = &quot;Reboot&quot;;<br />
 tm_setgo.Enabled = true;<br />
 }<br />
 if (rd_res.Checked == false)<br />
 {<br />
 pic_oper.Visible = true;<br />
 pic_opr.Visible = false;<br />
 }<br />
 }<br />
 private void rd_sleep_CheckedChanged(object sender, EventArgs e)<br />
 {<br />
 if (rd_sleep.Checked == true)<br />
 {<br />
 pic_oper.Visible = false;<br />
 pic_opr.Visible = true;<br />
 flag2 = true;<br />
 msg = &quot;Sleep&quot;;<br />
 tm_setgo.Enabled = true;<br />
 }<br />
 if (rd_sleep.Checked == false)<br />
 {<br />
 pic_oper.Visible = true;<br />
 pic_opr.Visible = false;<br />
 }<br />
 }<br />
 private void tm_setgo_Tick(object sender, EventArgs e)<br />
 {<br />
 string tm = (ud_hh.Text + &quot;:&quot; + ud_mm.Text + &quot;:&quot; + ud_ss.Text + &quot; &quot; + ud_pa.Text);<br />
 if (flag == true &amp;amp;&amp;amp; flag2 == true &amp;amp;&amp;amp; tm == Convert.ToString(DateTime.Now.ToLongTimeString()))<br />
 {<br />
 tm_setgo.Enabled = false;<br />
 if (rd_hiber.Checked == true)<br />
 {<br />
 frm_cess.Show();<br />
 Application.SetSuspendState(PowerState.Hibernate, true, true);<br />
 msg = &quot;Hibernate&quot;;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;<br />
 }<br />
 if (rd_lock.Checked == true)<br />
 {<br />
 cessation_task fm_cess=new cessation_task();<br />
 fm_cess.Show();<br />
 LockWorkStation();<br />
 msg = &quot;Lock&quot;;<br />
 }<br />
 if (rd_logoff.Checked == true)<br />
 {<br />
 cessation_task fm_cess = new cessation_task();<br />
 fm_cess.Show();<br />
 ExitWindowsEx(0, 0);<br />
 msg = &quot;Logoff&quot;;<br />
 }<br />
 if (rd_res.Checked == true)<br />
 {<br />
 cessation_task fm_cess = new cessation_task();<br />
 fm_cess.Show();<br />
 System.Diagnostics.Process.Start(&quot;shutdown&quot;, &quot;/r /f /t 0&quot;);<br />
 msg = &quot;Reboot&quot;;<br />
 }<br />
 if (rd_sleep.Checked == true)<br />
 {<br />
 cessation_task fm_cess = new cessation_task();<br />
 fm_cess.Show();<br />
 Application.SetSuspendState(PowerState.Suspend, true, true);<br />
 msg = &quot;Sleep&quot;;<br />
 }<br />
 if (rd_sh.Checked == true)<br />
 {<br />
 cessation_task fm_cess = new cessation_task();<br />
 fm_cess.Show();<br />
 System.Diagnostics.Process.Start(&quot;shutdown&quot;, &quot;/s /f /t 0&quot;);<br />
 msg = &quot;Shutdown&quot;;<br />
 }<br />
 }<br />
 }<br />
 private void nty_cess_MouseDoubleClick(object sender, MouseEventArgs e)<br />
 {<br />
 this.Show();<br />
 this.WindowState = FormWindowState.Normal;<br />
}<br />
 private void toolStripMenuItem1_Click(object sender, EventArgs e)<br />
 {<br />
 this.Hide();<br />
 }<br />
 private void toolStripMenuItem2_Click(object sender, EventArgs e)<br />
 {<br />
 this.Show();<br />
 this.WindowState = FormWindowState.Normal;<br />
 }<br />
 private void toolStripMenuItem3_Click(object sender, EventArgs e)<br />
 {<br />
 Application.Exit();<br />
 }<br />
 private void label6_Click(object sender, EventArgs e)<br />
 {<br />
 aboutme abm = new aboutme();<br />
abm.Show();<br />
 }<br />
private void pictureBox2_Click(object sender, EventArgs e)<br />
 {<br />
 aboutme abm = new aboutme();<br />
 abm.Show();<br />
 }<br />
 }<br />
}<br />

cessation_task.cs code

<br />
using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
namespace cessation<br />
{<br />
 public partial class cessation_task : Form<br />
 {<br />
 public cessation_task()<br />
 {<br />
 InitializeComponent();<br />
 }<br />
 private void cessation_task_Load(object sender, EventArgs e)<br />
 {<br />
 this.Opacity = 0.1;<br />
 Screen screen = Screen.PrimaryScreen;<br />
 int S_width = screen.Bounds.Width;<br />
 int S_height = screen.Bounds.Height;<br />
 this.Location = new Point((S_width - 240), (S_height-150));<br />
 timer1.Enabled = true;<br />
 timer1.Interval = 100;<br />
 }<br />
 public void msg(string msgg)<br />
 {<br />
 lbl_msg.Text = msgg;<br />
 }<br />
 private void timer1_Tick(object sender, EventArgs e)<br />
 {<br />
 if (this.Opacity &amp;lt; 1)<br />
 {<br />
 this.Opacity = this.Opacity + 0.1;<br />
 timer1.Enabled = true;<br />
 }<br />
 else<br />
 {<br />
 timer1.Enabled = false;<br />
 timer2.Enabled = true;<br />
 timer2.Interval = 3000;<br />
 }<br />
 }<br />
 private void button1_Click(object sender, EventArgs e)<br />
 {<br />
 this.Hide();<br />
 }<br />
 private void timer2_Tick(object sender, EventArgs e)<br />
 {<br />
 if (timer2.Interval == 3000)<br />
 timer3.Enabled = true;<br />
 }<br />
private void timer3_Tick(object sender, EventArgs e)<br />
 {<br />
 if (this.Opacity &gt; .1)<br />
 {<br />
 this.Opacity -= .1;<br />
 timer3.Enabled = true;<br />
 }<br />
 else<br />
 {<br />
 timer3.Enabled = false;<br />
 this.Close();<br />
 }<br />
 }<br />
 }<br />
}<br />

aboutme.cs code

<br />
using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
namespace cessation<br />
{<br />
 public partial class aboutme : Form<br />
 {<br />
 public aboutme()<br />
 {<br />
 InitializeComponent();<br />
 }<br />
 private void button1_Click(object sender, EventArgs e)<br />
 {<br />
 this.Close();<br />
 }<br />
 }<br />
}<br />

Advertisements