static class Program { /// <summary> /// Der Haupteinstiegspunkt für die Anwendung. /// </summary> [STAThread] static void Main(string[] args) { Mutex Mu = new Mutex(false, "{XEQRT97T57B1PNGJ1LRG83JEKGLUWX10}"); if (Mu.WaitOne(0, false)) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(true)); } else { // ADD MORE TIME TO THE ALREADY RUNNING TIMER } } }
public partial class Form1 : Form { public Int32 timeToShutdown = 0; public Int16 standardIntervalStep = 30; // in Minuten public Form1(Boolean addMoreMinutes) { if (addMoreMinutes == true) { InitializeComponent(); this.startMinimized(); } this.timeToShutdown = this.getTimeFromCommandLine(); this.setTimer(this.timeToShutdown); } public void startMinimized() { this.WindowState = FormWindowState.Minimized; this.ShowInTaskbar = false; } public Int32 getTimeFromCommandLine() { string[] args = Environment.GetCommandLineArgs(); foreach (String arg in args) { try { this.timeToShutdown += Convert.ToInt32(arg); } catch (Exception) { this.timeToShutdown += this.standardIntervalStep; } } // Es werden Minuten eingegeben, jetzt muss das in Millisekunden umgerechnet werden Int16 minutesToShutdown = Convert.ToInt16(timeToShutdown); timeToShutdown = timeToShutdown * 60 * 1000; DateTime shutdownTime = DateTime.Now.AddMilliseconds(timeToShutdown); this.systemTrayIcon.ShowBalloonTip(500, "Shutdown", "Windows wird in " + minutesToShutdown + " Minuten heruntergefahren. (" + shutdownTime + ")", ToolTipIcon.Info); return this.timeToShutdown; } public void setTimer(Int32 milliSeconds) { Timer shutdownTimer = new Timer(); shutdownTimer.Interval = milliSeconds; shutdownTimer.Tick += new EventHandler(exitWindows); shutdownTimer.Start(); } public void exitWindows(object sender, EventArgs e) { this.systemTrayIcon.ShowBalloonTip(500, "Shutdown", "Windows wird heruntergefahren.", ToolTipIcon.Info); StartShutDown("-f -s -t 5"); } private static void StartShutDown(string param) { ProcessStartInfo proc = new ProcessStartInfo(); proc.FileName = "cmd"; proc.WindowStyle = ProcessWindowStyle.Hidden; proc.Arguments = "/C shutdown " + param; Process.Start(proc); } private void closeShutdown_Click(object sender, EventArgs e) { Application.Exit(); }