科技行者

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

知识库

知识库 安全导航

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

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

  • 扫一扫
    分享文章到微信

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

   #endregion /// /// The main entry point for the application. /// [STAThread] static void

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

关键字: C# 使用 编程

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

  #endregion

  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new TimerComputerShutdown());
  }

  private void TimerComputerShutdown_Load(object sender, System.EventArgs e)
  {
   this.threadStart = new System.Threading.ThreadStart(Application_Tick);
   this.thread = new System.Threading.Thread(threadStart);
   
   this.panelChild.Visible = false;

   this.dateTimePicker.Value = System.DateTime.Now;
  }

  protected override void OnClosing(CancelEventArgs e)
  {
   try
   {
    if (this.thread.IsAlive)
    {
     this.thread.Abort();
     this.thread.Join();
    }
   }
   catch (System.Threading.ThreadAbortException threadAbortException)
   {
    System.Threading.Thread.ResetAbort();
   }
   catch (System.Exception exception)
   {
    
   }
   finally
   {
    System.Windows.Forms.Application.Exit();
   }

   base.OnClosing(e);
  }

  public void Computer_Shutdown()
  {
   if (this.checkBox.Checked)
   {
    System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses();

    foreach (System.Diagnostics.Process processParent in processes)
    {
     System.Diagnostics.Process[] processNames = System.Diagnostics.Process.GetProcessesByName(processParent.ProcessName);

     foreach (System.Diagnostics.Process processChild in processNames)
     {
      try
      {
       System.IntPtr hWnd = processChild.MainWindowHandle;
       
       if (IsIconic(hWnd))
       {
        ShowWindowAsync(hWnd, SW_RESTORE);
       }
     
       SetForegroundWindow(hWnd);
       
       if (!(processChild.MainWindowTitle.Equals(this.Text)))
       {
        processChild.CloseMainWindow();
        processChild.Kill();
        processChild.WaitForExit();
       }
      }
      catch (System.Exception exception)
      {

      }
     }
    }
   }

   System.Windows.Forms.Application.Exit();

   switch (this.comboBox.SelectedIndex)
   {
    case 0:
     WindowsController.ExitWindows(RestartOptions.PowerOff, false);
     break;
    case 1:
     WindowsController.ExitWindows(RestartOptions.Reboot, false);
     break;
    case 2:
     WindowsController.ExitWindows(RestartOptions.Suspend, false);
     break;
    case 3:
     WindowsController.ExitWindows(RestartOptions.Hibernate, false);
     break;
    case 4:
     WindowsController.ExitWindows(RestartOptions.LogOff, false);
     break;
   }
  }
  
  private void comboBox_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   this.labelSelectDateTimeToShutdownComputer.Text = "选择任务" + " " + this.comboBox.SelectedItem.ToString() + " " + "时间:";
   this.labelDateTimeShutdown.Text = "任务时间" + " " + this.comboBox.SelectedItem.ToString() + ":";
  }

  

  public bool dateTimePicker_Validated()
  {
   if (this.dateTimePicker.Value.CompareTo(System.DateTime.Now) < 0)
   {
    Error error = new Error();
    error.ShowDialog();

    this.dateTimePicker.Value = System.DateTime.Now;

    return false;
   }
   else
   {
    return true;
   }
  }

  private void dateTimePicker_ValueChanged(object sender, System.EventArgs e)
  {
   this.labelShutdown.Text = this.dateTimePicker.Value.ToString();
  }

  public void Application_Tick()
  {
   while (!(this.labelNow.Text.ToString().Equals(this.dateTimePicker.Value.ToString())))
   {
    this.labelNow.Text = System.DateTime.Now.ToString();
   }

   this.Computer_Shutdown();
  }

  private void buttonCancel_Click(object sender, System.EventArgs e)
  {
   try
   {
    if (this.thread.IsAlive)
    {
     this.thread.Abort();
     this.thread.Join();
    }
   }
   catch (System.Threading.ThreadStateException threadStateException)
   {
    System.Threading.Thread.ResetAbort();
   }
   catch (System.Exception exception)
   {
    
   }
   finally
   {
    System.Windows.Forms.Application.Exit();
   }
  }
  

 

查看本文来源

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

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

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