一个定时关机、注销程序

stjisu 2010-01-12 09:54:32
学了三个月C#,这是我第一个真正动过脑子的程序。目的是设置只允许儿子在星期六或星期天的下午用电脑2小时,而且每小时之间还要休息一段时间。我使用了Win7设置一个普通权限的用户名给儿子。并兴冲冲的设计了一个控制台程序,本以为一个EXE文件可以很绿色很环保,可是问题来了,不管怎么搞,程序都是执行后即退出DOS窗口,Timer也就不会继续计时,那怕1000毫秒都不计。以下为程序:


using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Timers;
using System.Runtime.InteropServices;
using System.Threading;



namespace ShutDownComputer
{
class Program
{

static void Main(string[] args)
{
string userName = Environment.UserName;
//识别用户名,进入星期判断
if (userName == "binhan")
Times.ShutdownOfWeek();

}
}
class Times
{
private static System.Timers.Timer aTimer;
//Timer事件
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
//一小时一到,即运行注销
Shutdown.DoExitWin(Shutdown.EWX_FORCE | Shutdown.EWX_LOGOFF);
}
//判断是否星期六、星期日,若是即进入时间区间判断
public static void ShutdownOfWeek()
{

int dayOfWeek = (int)DateTime.Now.DayOfWeek;
if (dayOfWeek == 6 || dayOfWeek == 0)

ShutdownOfTime();

else

Shutdown.DoExitWin(Shutdown.EWX_FORCE | Shutdown.EWX_LOGOFF);
}
//时间区间判断
public static void ShutdownOfTime()
{
//读取系统当前时间
DateTime NowTime = new DateTime();
NowTime = DateTime.Now;
//设置允许使用的时间区间
DateTime EachTime1 = new DateTime(NowTime.Year, NowTime.Month, NowTime.Day, 12, 30, 00);
DateTime EachTime2 = new DateTime(NowTime.Year, NowTime.Month, NowTime.Day, 13, 30, 00);
DateTime EachTime3 = new DateTime(NowTime.Year, NowTime.Month, NowTime.Day, 15, 30, 00);
DateTime EachTime4 = new DateTime(NowTime.Year, NowTime.Month, NowTime.Day, 16, 30, 00);

aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.AutoReset = false;
aTimer.Interval = 3600000;

if (NowTime >= EachTime1 && NowTime <= EachTime2)
{
aTimer.Enabled = true;

}
else
{
if (NowTime >= EachTime3 && NowTime <= EachTime4)
{
aTimer.Enabled = true;

}

else
Shutdown.DoExitWin(Shutdown.EWX_FORCE | Shutdown.EWX_LOGOFF);
}

}
}

//这是网上搜来的利用P/Invoke 调用窗口API的关机、重启、注销程序
class Shutdown
{
[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 flg, 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 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);
}
}
}

...全文
308 25 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuyu520hong 2010-01-16
  • 打赏
  • 举报
回复
谢谢分享!这个程序很值得一学
stjisu 2010-01-16
  • 打赏
  • 举报
回复
最终我采用了窗口方式,这个尝试非常有趣,从最最基础的winform的教程里,我只是学到放个textbox 再放个button,双击这个控件,放进代码就OK了,可是我这个程序就只放代码,应该放哪呢?
最终,我将循环放在load里,我蒙对了,程序运行正常,然后将窗口的Opacity设置为0 现在骗骗我家小孩应该能顶一阵子。
现在结帖,随便给捧场的朋友一点点分吧。
有兴趣的朋友可以看看我博客里的窗口完整代码。
http://blog.csdn.net/stjisu/archive/2010/01/15/5195892.aspx
stjisu 2010-01-12
  • 打赏
  • 举报
回复
中午有必要自顶一下!!!
nkboy 2010-01-12
  • 打赏
  • 举报
回复
用服务就简单了。
stjisu 2010-01-12
  • 打赏
  • 举报
回复
问题补充:我测试了整个流程,从用户名判断--到星期判断--到时间判断都能正常执行。但是在许可的时间区间,确实主线程是结束了,问题是如何才能保持主线程继续运行并计时呢?MSDN里的TIMER示例,放了一个Console.ReadLine() 呵呵。
放到CSDN来,目的还是为了向高手请教学习。
儿子会不会去结束掉,我并不担心。他如果能动动脑筋,我还有什么好担心的。
qldsrx 2010-01-12
  • 打赏
  • 举报
回复
就算程序一直开着,你儿子也可以关闭它,只要他动点脑筋。
你还不如设计一个一次性运行的程序,然后调用windows自带的计划任务来定时执行,并且给添加的计划任务设置访问权限,这样你儿子就无法停止或者删除该任务了。
nkboy 2010-01-12
  • 打赏
  • 举报
回复
你的程序并没有停留的语句 执行完了 肯定就完了呀 你用的是Timer线程,随着你主线程的结束timer会自动dispose掉 这样你开启的计划时间线程是无效的。
ysz89757 2010-01-12
  • 打赏
  • 举报
回复
不会,学习下
hzxsasdfgh 2010-01-12
  • 打赏
  • 举报
回复
那你CONSOLE.READKEY()或者console.readline()以下呀、
yccwt 2010-01-12
  • 打赏
  • 举报
回复
对你儿子也太狠了吧,周末都只能玩俩小时,呵呵。。

zdhook 2010-01-12
  • 打赏
  • 举报
回复
不要写控制台程序,做成WinForm.不然没有消息循环,你的窗口一下子就闪没了
dabendano 2010-01-12
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 erydemimi 的回复:]
为什么不做成windows服务?
[/Quote]
对,做成Windows服务,用timer时时的判断,符合条件关机OK
erydemimi 2010-01-12
  • 打赏
  • 举报
回复
为什么不做成windows服务?
aygrhnoyf 2010-01-12
  • 打赏
  • 举报
回复
谢谢。参考了。我还在学习中。
stjisu 2010-01-12
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 tommir3 的回复:]
没有细看,帮顶下吧。
希望你这个程序做好,其实不难,
不过还希望你儿子能破解它,
然后你再深入的修改,
然后你儿子再破解.....
我想不久的将来,你儿子会是个程序高手。
[/Quote]
对对对。本来就是这样想的。
tommir3 2010-01-12
  • 打赏
  • 举报
回复
没有细看,帮顶下吧。
希望你这个程序做好,其实不难,
不过还希望你儿子能破解它,
然后你再深入的修改,
然后你儿子再破解.....
我想不久的将来,你儿子会是个程序高手。
voacom 2010-01-12
  • 打赏
  • 举报
回复
这个要学习
风骑士之怒 2010-01-12
  • 打赏
  • 举报
回复
干嘛不做个C#窗体项目,隐藏其窗口到后台执行
leonhua2008 2010-01-12
  • 打赏
  • 举报
回复
很欣赏您的创造能力,能够把知识应用于实际生活,这个我也有过体会,很有成就感。这个程序还没有调试,先拜读一遍。
a12321321321312321 2010-01-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 stjisu 的回复:]
问题补充:我测试了整个流程,从用户名判断--到星期判断--到时间判断都能正常执行。但是在许可的时间区间,确实主线程是结束了,问题是如何才能保持主线程继续运行并计时呢?MSDN里的TIMER示例,放了一个Console.ReadLine() 呵呵。
放到CSDN来,目的还是为了向高手请教学习。
儿子会不会去结束掉,我并不担心。他如果能动动脑筋,我还有什么好担心的。

[/Quote]
说的不错,呵呵。
加载更多回复(5)

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧