科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道使用C#编写的一个定时关机程序之四

使用C#编写的一个定时关机程序之四

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

   private void buttonInvoke_Click(object sender, System.EventArgs e) { if (this.dateTimePicker_Validated()) {

作者:中国IT实验室 来源:中国IT实验室 2007年9月11日

关键字: C# 使用 编程

  • 评论
  • 分享微博
  • 分享邮件
  

  private void buttonInvoke_Click(object sender, System.EventArgs e)
  {
   if (this.dateTimePicker_Validated())
   {
    this.thread.Start();

    this.comboBox.Enabled = false;
    this.dateTimePicker.Enabled = false;
    this.panelChild.Visible = true;
    this.buttonInvoke.Enabled = false;
   }
  }
}
}

using System;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace TimerComputerShutdown{
/// <summary>
/// Specifies the type of restart options that an application can use.
/// </summary>
public enum RestartOptions {
  /// <summary>
  /// Shuts down all processes running in the security context of the process that called the ExitWindowsEx function. Then it logs the user off.
  /// </summary>
  LogOff = 0,
  /// <summary>
  /// Shuts down the system and turns off the power. The system must support the power-off feature.
  /// </summary>
  PowerOff = 8,
  /// <summary>
  /// Shuts down the system and then restarts the system.
  /// </summary>
  Reboot = 2,
  /// <summary>
  /// Shuts down the system to a point at which it is safe to turn off the power. All file buffers have been flushed to disk, and all running processes have stopped. If the system supports the power-off feature, the power is also turned off.
  /// </summary>
  ShutDown = 1,
  /// <summary>
  /// Suspends the system.
  /// </summary>
  Suspend = -1,
  /// <summary>
  /// Hibernates the system.
  /// </summary>
  Hibernate = -2,
}
/// <summary>
/// An LUID is a 64-bit value guaranteed to be unique only on the system on which it was generated. The uniqueness of a locally unique identifier (LUID) is guaranteed only until the system is restarted.
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct LUID {
  /// <summary>
  /// The low order part of the 64 bit value.
  /// </summary>
  public int LowPart;
  /// <summary>
  /// The high order part of the 64 bit value.
  /// </summary>
  public int HighPart;
}
/// <summary>
/// The LUID_AND_ATTRIBUTES structure represents a locally unique identifier (LUID) and its attributes.
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct LUID_AND_ATTRIBUTES {
  /// <summary>
  /// Specifies an LUID value.
  /// </summary>
  public LUID pLuid;
  /// <summary>
  /// Specifies attributes of the LUID. This value contains up to 32 one-bit flags. Its meaning is dependent on the definition and use of the LUID.
  /// </summary>
  public int Attributes;
}
/// <summary>
/// The TOKEN_PRIVILEGES structure contains information about a set of privileges for an access token.
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct TOKEN_PRIVILEGES {
  /// <summary>
  /// Specifies the number of entries in the Privileges array.
  /// </summary>
  public int PrivilegeCount ;
  /// <summary>
  /// Specifies an array of LUID_AND_ATTRIBUTES structures. Each structure contains the LUID and attributes of a privilege.
  /// </summary>
  public LUID_AND_ATTRIBUTES Privileges ;
}
/// <summary>
/// Implements methods to exit Windows.
/// </summary>
public class WindowsController {
  /// <summary>Required to enable or disable the privileges in an access token.</summary>
  private const int TOKEN_ADJUST_PRIVILEGES = 0x20;
  /// <summary>Required to query an access token.</summary>
  private const int TOKEN_QUERY = 0x8;
  /// <summary>The privilege is enabled.</summary>
  private const int SE_PRIVILEGE_ENABLED = 0x2;
  /// <summary>Specifies that the function should search the system message-table resource(s) for the requested message.</summary>
  private const int FORMAT_MESSAGE_FROM_SYSTEM = 0x1000;
  /// <summary>Forces processes to terminate. When this flag is set, the system does not send the WM_QUERYENDSESSION and WM_ENDSESSION messages. This can cause the applications to lose data. Therefore, you should only use this flag in an emergency.</summary>
  private const int EWX_FORCE = 4;
  /// <summary>
  /// The LoadLibrary function maps the specified executable module into the address space of the calling process.
  /// </summary>
  /// <param name="lpLibFileName">Pointer to a null-terminated string that names the executable module (either a .dll or .exe file). The name specified is the file name of the module and is not related to the name stored in the library module itself, as specified by the LIBRARY keyword in the module-definition (.def) file.</param>
  /// <returns>If the function succeeds, the return value is a handle to the module.<br></br><br>If the function fails, the return value is NULL. To get extended error information, call Marshal.GetLastWin32Error.</br></returns>
  [ DllImport( "kernel32.dll", EntryPoint="LoadLibraryA", CharSet=CharSet.Ansi )]
  private static extern IntPtr LoadLibrary(string lpLibFileName);
  /// <summary>
  /// The FreeLibrary function decrements the reference count of the loaded dynamic-link library (DLL). When the reference count reaches zero, the module is unmapped from the address space of the calling process and the handle is no longer valid.
  /// </summary>
  /// <param name="hLibModule">Handle to the loaded DLL module. The LoadLibrary or GetModuleHandle function returns this handle.</param>
  /// <returns>If the function succeeds, the return value is nonzero.<br></br><br>If the function fails, the return value is zero. To get extended error information, call Marshal.GetLastWin32Error.</br></returns>
  [ DllImport( "kernel32.dll", EntryPoint="FreeLibrary", CharSet=CharSet.Ansi )]
  private static extern int FreeLibrary(IntPtr hLibModule);

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章