Automatic shutdown


Here is the sample application developed in Windows c# .net to shutdown your system automatically when your system is idle for long time.This is very interesting Program you can find the nice experience by Knowing it.



Here i Explained the magic coding to shutdown your system Automatically




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace SensorShutDown
{
public partial class Form1 : Form
{
int j = 600;
// Unmanaged function from user32.dll
[DllImport("user32.dll")]
//Geting last userinput
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
// Struct we'll need to pass to the function
internal struct LASTINPUTINFO
{
public uint cbSize;
public uint dwTime;
}

public Form1()
{

InitializeComponent();
}

private void tmrIdle_Tick(object sender, EventArgs e)
{
//setting default idletime to 600 sec to shutdown
int i = 60;

// Get the system uptime
int systemUptime = Environment.TickCount;
// The tick at which the last input was recorded
int LastInputTicks = 0;
// The number of ticks that passed since last input
int IdleTicks = 0;

// Set the struct
LASTINPUTINFO LastInputInfo = new LASTINPUTINFO();
LastInputInfo.cbSize = (uint)Marshal.SizeOf(LastInputInfo);
LastInputInfo.dwTime = 0;

// If we have a value from the function
if (GetLastInputInfo(ref LastInputInfo))
{
// Get the number of ticks at the point when the last activity was seen
LastInputTicks = (int)LastInputInfo.dwTime;
// Number of idle ticks = system uptime ticks - number of ticks at last input
IdleTicks = systemUptime - LastInputTicks;
}

// Set the labels; divide by 1000 to transform the milliseconds to seconds
lblSystemUptime.Text = Convert.ToString(systemUptime / 1000) + " seconds";
lblIdleTime.Text = Convert.ToString(IdleTicks / 1000) + " seconds";
lblLastInput.Text = "At second " + Convert.ToString(LastInputTicks / 1000);

if (textBox1.Text == " ")
{
try
{
while ((IdleTicks / 1000) == i)
{
System.Diagnostics.Process.Start("shutdown.exe",-s);
break;
}
}
catch (System.ComponentModel.Win32Exception)
{
}
}
//here we setting our own seconds to shut down
else
{
try
{

while ((IdleTicks / 1000) == j)
{
System.Diagnostics.Process.Start("shutdown.exe",-s);
break;
}
}
catch (System.ComponentModel.Win32Exception)
{

}

}
}

private void Form1_Load(object sender, EventArgs e)
{
button1.Visible = false;
textBox1.Visible = false;
}


private void button1_Click_1(object sender, EventArgs e)
{

button1.Visible = false;
textBox1.Visible = false;
try
{

j = Convert.ToInt32(textBox1.Text);
}
catch (FormatException)
{
MessageBox.Show("Please enter numeric value");
}
}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
button1.Visible = true;
textBox1.Visible = true;
}
else
{

button1.Visible = false; ;
textBox1.Visible = false;
}
}



}
}
}


Comments

Guest Author: Chris03 May 2012

Very Helpful. Thank you very much!

Guest Author: Mohanasundaram14 Dec 2012

Hi here i have one error 's' does not exist in the current content.... how to solve this problem.....tel immediately



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: