{"id":36233,"date":"2005-03-10T06:58:00","date_gmt":"2005-03-10T06:58:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2005\/03\/10\/why-does-systemparametersinfo-hang-when-i-pass-the-spif_sendchange-flag\/"},"modified":"2005-03-10T06:58:00","modified_gmt":"2005-03-10T06:58:00","slug":"why-does-systemparametersinfo-hang-when-i-pass-the-spif_sendchange-flag","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050310-00\/?p=36233","title":{"rendered":"Why does SystemParametersInfo hang when I pass the SPIF_SENDCHANGE flag?"},"content":{"rendered":"<p>\nIf you pass the <code>SPIF_SENDCHANGE<\/code> flag to\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/sysinfo\/base\/systemparametersinfo.asp\">\nthe <code>SystemParametersInfo<\/code> function<\/a>,\nit will broadcast\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/sysinfo\/base\/wm_settingchange.asp\">\nthe <code>WM_SETTINGCHANGE<\/code> message<\/a>\nwith the wParam equal to the system parameter code you passed.\nFor example, if you call\n<\/p>\n<pre>\nSystemParametersInfo(SPI_SETDOUBLECLICKTIME,\n      500, 0, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);\n<\/pre>\n<p>\nthen the system will broadcast the message\n<\/p>\n<pre>\nSendMessage(HWND_BROADCAST, WM_SETTINGCHANGE,\n            SPI_SETDOUBLECLICKTIME, 0);\n<\/pre>\n<p>\nIf there is a window that isn&#8217;t responding to messages,\nthen this broadcast will hang until that unresponsive\nwindow finally resumes responding to messages or is killed.\n<\/p>\n<p>\nIf you&#8217;d rather not be victimed by unresponsive windows,\nyou have a few options, but it also may affect your program&#8217;s\nexpectations.\n<\/p>\n<p>\nYou could issue the <code>SystemParametersInfo<\/code>\ncall on a background thread.  Then your background thread\nis the one that blocks instead of your UI thread.\n<\/p>\n<p>\nWith this message, the background thread can notify the\nmain thread when the broadcast finally completes, at which\npoint your program now knows that all windows have received\ntheir notifications and are on board with the new setting.\n<\/p>\n<p>\nYou could issue the <code>SystemParametersInfo<\/code>\ncall without the <code>SPIF_SENDCHANGE<\/code> flag,\nthen manually broadcast the change via\n<\/p>\n<pre>\nDWORD dwResult;\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/winui\/windowsuserinterface\/windowing\/messagesandmessagequeues\/messagesandmessagequeuesreference\/messagesandmessagequeuesfunctions\/sendmessagetimeout.asp\">SendMessageTimeout<\/a>(HWND_BROADCAST, WM_SETTINGCHANGE,\n            SPI_SETDOUBLECLICKTIME, 0,\n            SMTO_ABORTIFHUNG | SMTO_NOTIMEOUTIFNOTHUNG,\n            5000, &amp;dwResult);\n<\/pre>\n<p>\nThis does mean that unresponsive windows will not receive the\nnotification that a system parameter has changed.\nThis is acceptable if you decide\nthat your change in settings was minor enough\nthat a program missing the notification is no big deal.\nIn other words, when the unresponsive program finally wakes up,\nit will not know that the setting has changed since it missed\nthe notification.\n<\/p>\n<p>\nYou can combine the above two methods: Use a background thread\nand send the message with a timeout.\n<\/p>\n<p>\nPerhaps the best technique is to use\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/WinUI\/WindowsUserInterface\/Windowing\/MessagesandMessageQueues\/MessagesandMessageQueuesReference\/MessagesandMessageQueuesFunctions\/SendNotifyMessage.asp\">\nthe <code>SendNotifyMessage<\/code> function<\/a>.\n<a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/11\/19\/266664.aspx\">\nAs we learned earlier<\/a>,\nthe <code>SendNotifyMessage<\/code> function\nis like <code>SendMessage<\/code> except that it doesn&#8217;t wait\nfor a response.  This lets your program get back work while\nnot messing up programs that were momentarily unresponsive\nwhen you decided to broadcast the notification.\n<\/p>\n<pre>\nSendNotifyMessage(HWND_BROADCAST, WM_SETTINGCHANGE,\n            SPI_SETDOUBLECLICKTIME, 0);\n<\/pre>\n<p>\nThe downside is that you don&#8217;t know when all windows\nhave finally received and processed the notification.\nAll you know is that someday, they will eventually find out.\nUsually you don&#8217;t care about this aspect of the broadcast,\nso this lack of information is not an impediment.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you pass the SPIF_SENDCHANGE flag to the SystemParametersInfo function, it will broadcast the WM_SETTINGCHANGE message with the wParam equal to the system parameter code you passed. For example, if you call SystemParametersInfo(SPI_SETDOUBLECLICKTIME, 500, 0, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE); then the system will broadcast the message SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, SPI_SETDOUBLECLICKTIME, 0); If there is a window that [&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-36233","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>If you pass the SPIF_SENDCHANGE flag to the SystemParametersInfo function, it will broadcast the WM_SETTINGCHANGE message with the wParam equal to the system parameter code you passed. For example, if you call SystemParametersInfo(SPI_SETDOUBLECLICKTIME, 500, 0, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE); then the system will broadcast the message SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, SPI_SETDOUBLECLICKTIME, 0); If there is a window that [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/36233","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=36233"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/36233\/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=36233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=36233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=36233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}