public void Dispose() { // Enter Shutdown State. disposeLocking.EnterWriteLock(); try { disposing = true; } finally { disposeLocking.ExitWriteLock(); } // Shutdown Operations Executor. var shutdownOperation = new ShutdownOperation(); operations.Add(shutdownOperation); operationsExecutor.Join(); // Clean up. operations.Dispose(); disposeLocking.Dispose(); backed.Dispose(); }
/// <summary> /// 指定如何进行系统关机. /// </summary> /// <param name="so"></param> public static void ShutDown(ShutdownOperation so) { Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start(); switch (so) { case ShutdownOperation.ShutDown: try { p.StandardInput.WriteLine("shutdown -f -s -t 0"); p.StandardInput.WriteLine("exit"); } catch (Exception e) { MessageBox.Show(e.Message, "警告", MessageBoxButtons.OK); break; } break; case ShutdownOperation.Reboot: try { p.StandardInput.WriteLine("shutdown -f -r -t 0"); p.StandardInput.WriteLine("exit"); } catch (Exception e) { MessageBox.Show(e.Message, "警告", MessageBoxButtons.OK); break; } break; case ShutdownOperation.Logoff: try { p.StandardInput.WriteLine("shutdown -l"); p.StandardInput.WriteLine("exit"); } catch (Exception e) { MessageBox.Show(e.Message, "警告", MessageBoxButtons.OK); break; } break; case ShutdownOperation.Hibernate: try { p.StandardInput.WriteLine("powercfg -h on"); Application.SetSuspendState(PowerState.Hibernate, true, true); } catch (Exception e) { MessageBox.Show(e.Message, "警告", MessageBoxButtons.OK); break; } break; case ShutdownOperation.Suspend: try { Application.SetSuspendState(PowerState.Suspend, true, false); } catch (Exception e) { MessageBox.Show(e.Message, "警告", MessageBoxButtons.OK); break; } break; case ShutdownOperation.LockWorkStation: try { LockWorkStation(); } catch (Exception e) { MessageBox.Show(e.Message, "警告", MessageBoxButtons.OK); break; } break; } }