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
| public partial class Form1 : Form
{
private static bool SessionLock = false;
private System.Threading.Timer timer = new System.Threading.Timer(OnTimer,null,1000,1000);
private static StreamWriter writer;
public Form1()
{
InitializeComponent();
SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
writer = new StreamWriter(@"d:\trace.txt");
writer.AutoFlush = true;
}
static void OnTimer(object state)
{
if (SessionLock)
{
writer.WriteLine("OnTimer Session Locked : Tick at " + DateTime.Now.Second.ToString());
}
else
{
writer.WriteLine("OnTimer Session UnLocked : Tick at " + DateTime.Now.Second.ToString());
}
}
void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
if (e.Reason == SessionSwitchReason.SessionLock)
{
writer.WriteLine("Session is lock at " + DateTime.Now.ToString());
SessionLock = true;
}
if (e.Reason == SessionSwitchReason.SessionUnlock)
{
writer.WriteLine("Session is unlock at " + DateTime.Now.ToString());
SessionLock = false;
}
}
} |
Partager