我写的定时关机的程序。但是却不能定时,帮忙看一下,好像是timer那些地方有问题还是。。
要定时关机、重启等等
不定时的话,关机重启都没问题,
加入定时就出问题了
好像是定时了之后,判断那出现问题,反正就是时间到了执行不了关机
谁帮我一下哦
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
public Form1()
{
InitializeComponent();
}
//设置计时变量
public static int hour = 0;
public static int minute = 0;
public static int second = 0;
public void button1_Click(object sender, EventArgs e)
{
//开始计时,变量归零
second = 0;
minute = 0;
hour = 0;
API ACT = new API();
string 选择=Convert.ToString(cbStyle.SelectedItem);
if(选择=="定时重启")
{
//比较时间是否到了
if (Convert.ToString(cbHour.SelectedItem) == hour.ToString() && Convert.ToString(cbMin.SelectedItem) == minute.ToString() && Convert.ToString(cbSec.SelectedItem) == second.ToString())
ACT.Reboot();
/*System.Diagnostics.Process bootprocess=new System .Diagnostics.Process ();
bootprocess.StartInfo.FileName="reboot";
bootprocess.StartInfo.Arguments="/s";*/
}
if(选择=="定时关机")
{
// Sets the timer interval to 5 seconds.
if(Convert.ToString(cbHour.SelectedItem) == hour.ToString() && Convert.ToString(cbMin.SelectedItem) == minute.ToString() && Convert.ToString(cbSec.SelectedItem) ==second.ToString())
ACT.PowerOff();
/*System.Diagnostics.Process bootprocess=new System .Diagnostics.Process ();
bootprocess.StartInfo.FileName="shutdown";
bootprocess.StartInfo.Arguments="/s";*/
}
/* if(选择=="定时睡眠")
{
}*/
}
public void timer1_Tick(object sender, EventArgs e)
{
//计时变量自增
second++;
if (second == 60)
{
minute++;
second = 0;
}
if (minute == 60)
{
hour++;
minute = 0;
}
}
public void cbHour_SelectedIndexChanged(object sender, EventArgs e)
{
//下拉选择关机时间
cbMin.Items.Clear();
cbSec.Items.Clear();
for (int i = 0; i <=59; i++)
{
cbMin.Items.Add(i);
cbSec.Items.Add(i);
}
}
private void dTP_Now_ValueChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
//关机、重启的命令(copy别人的,初学者不大会写这个)
public class API
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}
[DllImport("kernel32.dll", ExactSpelling = true)]
internal static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid);
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool ExitWindowsEx(int DoFlag, int rea);
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
internal const int EWX_FORCEIFHUNG = 0x00000010;
public static void DoExitWin(int DoFlag)
{
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
ok = ExitWindowsEx(DoFlag, 0);
}
public void Reboot() //重启
{
DoExitWin(EWX_FORCE | EWX_REBOOT);
}
public void PowerOff() //关机
{
DoExitWin(EWX_FORCE | EWX_POWEROFF);
}
public void LogOff() //注销
{
DoExitWin(EWX_FORCE | EWX_LOGOFF);
}
}
}
[解决办法]
给个代码片段给你看看
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private static void DoExitWin(int flg)
{
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok );
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid );
ok = AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero );
ok = ExitWindowsEx( flg, 0 );
}
private void Form1_Load(object sender, System.EventArgs e)
{
this.notifyIcon1.Visible=true;
DateTime shutdownTime=DateTime.Now;
www.shiapifa.com
txtTime.Text=System.DateTime.Now.ToString();
dtpDay1.Format = DateTimePickerFormat.Custom;
dtpDay1.CustomFormat = "yyyy-MM-d";
dtpTime1.Format = DateTimePickerFormat.Custom;
dtpTime1.CustomFormat = "HH:mm:ss";
}
private void timer1_Tick(object sender, System.EventArgs e)
{
txtTime.Text=System.DateTime.Now.ToString();
string CurrDate=System.DateTime.Today.ToShortDateString() ;
string CurrTime=System.DateTime.Now.ToLongTimeString();
if(CurrDate==dtpDay1.Value.ToShortDateString() && CurrTime==dtpTime1.Value.ToLongTimeString())
{
if(rb_reboot.Checked==true)
{
DoExitWin(EWX_REBOOT);
Application.Exit();
}
else
{
DoExitWin(EWX_SHUTDOWN);
Application.Exit();
}
}
}
private void btnOK_Click(object sender, System.EventArgs e)
{
this.Visible=false;
this.notifyIcon1.Visible=true;
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
notifyIcon1.Visible=false;
this.Close();
Application.Exit();
}
private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
{
this.Visible=true;
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) www.wanxinyi.com
{
e.Cancel=true;
this.Visible=false;
this.notifyIcon1.Visible=true;
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
this.Visible=false;
notifyIcon1.Visible=false;
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
notifyIcon1.Visible=false;
this.Close();
Application.Exit();
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
Form2 frm2=new Form2();
frm2.Show();
}
private void menuItem4_Click(object sender, System.EventArgs e)
{
DoExitWin(EWX_POWEROFF);
Application.Exit();
}
private void menuItem5_Click(object sender, System.EventArgs e)
{
DoExitWin(EWX_REBOOT);
Application.Exit();
}
bool isNumberic(string s)
{
try
{
int.Parse(s);
return true;
}
catch
{
return false;
}
}
private void rb_reboot_CheckedChanged(object sender, System.EventArgs e)
{
if(rb_reboot.Checked==true)
{
rb_shutdown.Checked=false;
}
else
{
rb_shutdown.Checked=true;
}
}
private void rb_shutdown_CheckedChanged(object sender, System.EventArgs e)
{
if(rb_shutdown.Checked==true)
{
rb_reboot.Checked=false;
}
else
{
rb_reboot.Checked=true;
}
}
}
}