{"id":32783,"date":"2006-01-04T09:58:50","date_gmt":"2006-01-04T09:58:50","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2006\/01\/04\/taxes-detecting-session-state-changes-such-as-a-locked-workstation\/"},"modified":"2006-01-04T09:58:50","modified_gmt":"2006-01-04T09:58:50","slug":"taxes-detecting-session-state-changes-such-as-a-locked-workstation","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20060104-50\/?p=32783","title":{"rendered":"Taxes: Detecting session state changes, such as a locked workstation"},"content":{"rendered":"<p>\nAnother developer tax is playing friendly with Fast User Switching\nand Terminal Services.\nWhen the workstation is locked or disconnected,\nyou should turn off non-essential timers,\nminimize background activities,\nand generally send your program into a quiet state.\nIf you already used the technique of\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2003\/08\/29\/54728.aspx\">\npainting only when your window is visible on the screen<\/a>,\nthen you get all of this for free,\nsince a locked workstation and a disconnected session do not\ngenerate paint messages.\n<\/p>\n<p>\nIf you have other activities that you need to scale back or\nshut down when the user has locked the workstation or disconnected,\nyou can register to be notified when the state changes.\nKnowing about these state changes is also important so that\nyou can tell when your display is local or remote.\nAs we saw last time,\ndrawing on Remote Desktop Connection is much slower than on a local\ndisplay, since all the bitmaps need to be transferred over the\nnetwork to the Remote Desktop client.\n<\/p>\n<p>Since locking a workstation and disconnecting a session remove the\nability to use visual feedback to indicate our program&#8217;s state,\nwe&#8217;ll use the speaker.\nStart with our\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/04\/22\/410773.aspx\">\nnew scratch program<\/a>\nand make the following changes:\n<\/p>\n<pre>\n<font COLOR=\"blue\">#include &lt;wtsapi32.h&gt;<\/font>\nLRESULT RootWindow::OnCreate()\n{\n <font COLOR=\"blue\">WTSRegisterSessionNotification(m_hwnd, NOTIFY_FOR_THIS_SESSION);<\/font>\n return 0;\n}\nLRESULT RootWindow::HandleMessage(\n                          UINT uMsg, WPARAM wParam, LPARAM lParam)\n{\n switch (uMsg) {\n ...\n <font COLOR=\"blue\">case WM_WTSSESSION_CHANGE:\n  switch (wParam) {\n  case WTS_CONSOLE_DISCONNECT:\n  case WTS_REMOTE_DISCONNECT:\n  case WTS_SESSION_LOCK:\n  case WTS_SESSION_LOGOFF:\n   Beep(440, 250); break;\n  case WTS_CONSOLE_CONNECT:\n  case WTS_REMOTE_CONNECT:\n  case WTS_SESSION_UNLOCK:\n  case WTS_SESSION_LOGON:\n   Beep(880, 250); break;\n  }\n  break;<\/font>\n ...\n}\n<\/pre>\n<p>\nIn this program, we register for a session notification when\nwe create our main window,\nand listen for the session change message in our window procedure.\nIf we see one of the &#8220;going away&#8221; messages, we make a low beep;\nif we see one of the &#8220;coming back&#8221; messages, we make a high beep.\n<\/p>\n<p>\nRun this program and then lock the workstation or\nuse Fast User Switching to switch away.\nYou should be greeted by a low beep (though you may have to listen\ncarefully if you have a sound associated with the action you\nperformed because the low beep will be mixed in with it).\nWhen you switch back, you should hear a high beep.\n<\/p>\n<p>\nOf course, a real program would respond to the notifications by\nstarting or stopping its background tasks.\nThe purpose of this program was merely to show how to get the\nnotifications in the first place.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Another developer tax is playing friendly with Fast User Switching and Terminal Services. When the workstation is locked or disconnected, you should turn off non-essential timers, minimize background activities, and generally send your program into a quiet state. If you already used the technique of painting only when your window is visible on the screen, [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-32783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Another developer tax is playing friendly with Fast User Switching and Terminal Services. When the workstation is locked or disconnected, you should turn off non-essential timers, minimize background activities, and generally send your program into a quiet state. If you already used the technique of painting only when your window is visible on the screen, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/32783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=32783"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/32783\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=32783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=32783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=32783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}