{"id":12463,"date":"2010-10-25T07:00:00","date_gmt":"2010-10-25T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2010\/10\/25\/when-you-call-a-function-your-code-doesnt-resume-execution-until-that-function-returns\/"},"modified":"2010-10-25T07:00:00","modified_gmt":"2010-10-25T07:00:00","slug":"when-you-call-a-function-your-code-doesnt-resume-execution-until-that-function-returns","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20101025-00\/?p=12463","title":{"rendered":"When you call a function, your code doesn&#039;t resume execution until that function returns"},"content":{"rendered":"<p>\nConsider this code fragment:\n<\/p>\n<pre>\nvoid foo()\n{\n  while (true) {\n   bar();\n   baz();\n  }\n}\n<\/pre>\n<p>\nWhen <code>foo<\/code> calls <code>bar()<\/code>,\nand <code>bar<\/code> has not yet returned, does <code>foo<\/code>\ncontinue executing?\nDoes <code>baz<\/code> get called before <code>bar<\/code> returns?\n<\/p>\n<p>\nNo, it does not.\n<\/p>\n<p>\nThe basic structure of the C\/C++ language imposes sequential execution.\nControl does not return to the <code>foo<\/code> function\nuntil <code>bar<\/code> returns control,\neither by reaching the end of the function\nor by an explicit <code>return<\/code>.\n<\/p>\n<p>\nCommenter Norman Diamond\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/pages\/407234.aspx#1972314\">\nasks a bunch of questions<\/a>, but\nthey&#8217;re all mooted by the first:\n<\/p>\n<blockquote CLASS=\"q\">\n<p>\nI can&#8217;t find any of the answers in MSDN,\nand even an answer to one doesn&#8217;t make answers to others obvious.\n<\/p>\n<p>Unless failures occur,\nthe DialogBox function doesn&#8217;t return\nuntil the new dialog&#8217;s DialogProc calls EndDialog.\nIt starts its own message loop.\nDkring this time the hwndParent (i.e. owner not parent) window is disabled.\nHowever, disabling doesn&#8217;t prevent delivery of some kinds of messages\nto the parent window&#8217;s WindowProc or DialogProc,\nand doesn&#8217;t prevent delivery of any messages\nto the application&#8217;s main message loop, right?\nSo aren&#8217;t there two or more message loops running in parallel?\n<\/p>\n<\/blockquote>\n<p>\nAs long as the function <code>DialogBox<\/code> has not yet returned,\ncontrol does not return to the application&#8217;s main message loop,\nsince it is the one which called <code>DialogBox<\/code> (most likely\nindirectly).\n<\/p>\n<p>\nMSDN doesn&#8217;t explain this because\nit is a fundamental property of the C and C++ languages and is not\npeculiar to Win32.\n<\/p>\n<p>\nDisabling a window does not prevent it from receiving messages\nin general; it only disables mouse and keyboard input.\nThis is called out in the opening sentence of the <code>EnableWindow<\/code>\nfunction documentation:\n<\/p>\n<blockquote CLASS=\"m\"><p>\nThe <b>EnableWindow<\/b> function enables or disables mouse\nand keyboard input to the specified window or control.\n<\/p><\/blockquote>\n<p>\nMessages unrelated to mouse and keyboard input are delivered normally.\nAnd they aren&#8217;t dispatched by the application&#8217;s main message loop\nbecause, as we saw above, the main message loop isn&#8217;t executing!\n<\/p>\n<p>\nI would recommend reviewing a book that covers\nthe basics of Win32 GUI programming, since there appear to be some\nfundamental misunderstandings.\nSince I try to target an advanced audience, I generally assume\nthat everybody understands the basics and is ready to move on\nto the intermediate and advanced topics.\nIf you have trouble with the basics, you should work on that part first.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Consider this code fragment: void foo() { while (true) { bar(); baz(); } } When foo calls bar(), and bar has not yet returned, does foo continue executing? Does baz get called before bar returns? No, it does not. The basic structure of the C\/C++ language imposes sequential execution. Control does not return to the [&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-12463","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Consider this code fragment: void foo() { while (true) { bar(); baz(); } } When foo calls bar(), and bar has not yet returned, does foo continue executing? Does baz get called before bar returns? No, it does not. The basic structure of the C\/C++ language imposes sequential execution. Control does not return to the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/12463","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=12463"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/12463\/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=12463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=12463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=12463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}