Hurrah – after a two and a half weeks being stuck at home, the doc tells me that my back is well enough for me to go back to work tomorrow.
I’m not fully recovered – the estimate for that is still 6 – 8 weeks, so I’m been warned to take it easy for a few weeks. In particular, he made it very clear that I must get up and stretch my legs regularly, as prolonged periods sitting could set my recovery back.
Unfortunately, I know I have a habit of being a bit single-minded (some might say “obsessive” 😉 ) while I’m working, and often lose track of time. So I’ve thrown together a 35-line program to help make sure I don’t overdo it.
It will automatically lock my ThinkPad after 40 minutes, resetting the timer each time I unlock the ThinkPad again.
It’s an interesting, if trivial app. The code is below. Hopefully it’ll help! 🙂
private int countdownmins = MINS_TO_WORK; private const string MSG_PREFIX = "You need to get up and stretch your legs in "; public Form1() { InitializeComponent(); timer1.Interval = ONE_MINUTE; SetDisplay(); WTSRegisterSessionNotification(Handle, NOTIFY_FOR_THIS_SESSION); } private void SetDisplay() { label1.Text = MSG_PREFIX + countdownmins + " minutes"; } private void timer1_Tick(object sender, EventArgs e) { countdownmins -= 1; SetDisplay(); if (countdownmins == 0) { LockWorkStation(); } } protected override void WndProc(ref Message m) { if ((m.Msg == WM_WTSSESSION_CHANGE) && (m.WParam.ToInt32() == WTS_SESSION_UNLOCK)) { countdownmins = MINS_TO_WORK; SetDisplay(); } base.WndProc(ref m); } private const int MINS_TO_WORK = 40; private const int ONE_MINUTE = 60000; private const int NOTIFY_FOR_THIS_SESSION = 0; private const int WM_WTSSESSION_CHANGE = 0x2b1; private const int WTS_SESSION_LOCK = 0x7; private const int WTS_SESSION_UNLOCK = 0x8; [DllImport("user32.dll")] internal static extern bool LockWorkStation(); [DllImport("WtsApi32.dll", SetLastError = true)] internal static extern bool WTSRegisterSessionNotification (IntPtr hWnd, [MarshalAs(UnmanagedType.U4)] int dwFlags);
Tags: back, lock, lockworkstation, work
You should really learn IronPython! It makes no sense to use C# for all those little script/apps!
I’ve not heard of IronPython before – will have to take a look.
Scripting it is a good point though – in hindsight, I could have easily written it in Powershell, and that would probably have been better. Ah well, next time 🙂
@Alcides – I had a quick look at IronPython – it looks very interesting. Do you know how I could have made the timer start when the ThinkPad is unlocked? (e.g. the equivalent of calling
WTSRegisterSessionNotification
)I suppose this is Managed Code (although I’m not sure).
If it is, then you just add to the clr the Assemblies you need. ( clr.AddReference ) and then just call the same function.
If this is unmanaged code, take a look at:
http://www.ironpython.info/index.php/Access_Unmanaged_Code_with_Dynamic_P/Invoke
Hope I helped.