-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShutdownTimer.cs
67 lines (57 loc) · 1.6 KB
/
ShutdownTimer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace ComputerShutdown
{
class ShutdownTimer
{
public Timer Timer { get; set; }
public Dep Dep
{
get;
set;
}
public DateTime endTime
{
get;
set;
}
public ShutdownTimer() {
this.Timer = new Timer();
this.Dep = Dep.GetInstance();
}
public bool StartTimer(int secend)
{
this.Timer.Interval = 1000;
this.Timer.Enabled = true;
this.Timer.Elapsed += (object sender, ElapsedEventArgs e) => this.Timer_Elapsed(sender, e, secend);
this.endTime = DateTime.Now.AddSeconds(secend);
this.Timer.Start();
return true;
}
public bool StopTimer()
{
this.Timer.Stop();
return true;
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e, int secend)
{
long cTime = DateTime.Now.Ticks;
long eTime = this.endTime.Ticks;
if (cTime >= eTime)
{
this.Timer.Stop();
// 执行关机
var isSuccess = WindowAPI.DoExitWin(WindowAPI.EWX_FORCE | WindowAPI.EWX_SHUTDOWN);
if (!Convert.ToBoolean(isSuccess))
{
WindowAPI.GetLastError();
}
}
this.Dep.Emit(eTime - cTime);
}
}
}