Note that there are some explanatory texts on larger screens.

plurals
  1. POMouse jiggling / message processing loop
    primarykey
    data
    text
    <p>I have written a multithreaded program which does some thinking and prints out some diagnostics along the way. I have noticed that if I jiggle the mouse while the program is running then the program runs quicker. Now I could go in to detail here about how exactly I'm printing... but I will hold off just for now because I've noticed that in many other programs, things happen faster if the mouse is jiggled, I wonder if there is some classic error that many people have made in which the message loop is somehow slowed down by a non-moving mouse.</p> <p>EDIT: My method of "printing" is as follows... I have a rich edit control window to display text. When I want to print something, I append the new text on to the existing text within the window and then redraw the window with SendMessage(,WM_PAINT,0,0).</p> <p>Actually its a bit more complicated, I have multiple rich edit control windows, one for each thread (4 threads on my 4-core PC). A rough outline of my "my_printf()" is as follows:</p> <pre><code>void _cdecl my_printf(char *the_text_to_add) { EnterCriticalSection(&amp;my_printf_critsec); GetWindowText(...); // get the existing text SetWindowText(...); // append the_text_to_add SendMessage(...WM_PAINT...); LeaveCriticalSection(&amp;my_printf_critsec); } </code></pre> <p>I should point out that I have been using this method of printing for years in a non-multithreaded program without even noticing any interaction with mouse-jiggling.</p> <p>EDIT: Ok, here's my entire messageloop that runs on the root thread while the child threads do their work. The child threads call my_printf() to report on their progress.</p> <pre><code>for(;;) { DWORD dwWake; MSG msg; dwWake = MsgWaitForMultipleObjects( current_size_of_handle_list, hThrd, FALSE, INFINITE, QS_ALLEVENTS); if (dwWake &gt;= WAIT_OBJECT_0 &amp;&amp; dwWake &lt; (WAIT_OBJECT_0 + current_size_of_handle_list)) { int index; index = dwWake - WAIT_OBJECT_0; int j; for (j = index+1;j &lt; current_size_of_handle_list;j++) { hThrd[j-1] = hThrd[j]; } current_size_of_handle_list--; if (current_size_of_handle_list == 0) { break; } } else if (dwWake == (WAIT_OBJECT_0 + current_size_of_handle_list)) { while (PeekMessage(&amp;msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&amp;msg); DispatchMessage(&amp;msg); } } else if (dwWake == WAIT_TIMEOUT) { printmessage("TIMEOUT!"); } else { printmessage("Goof!"); } } </code></pre> <p>EDIT: Solved! This may be an ugly solution - but I just changed the timeout from infinite to 20ms, then in the if (dwWake == WAIT_TIMEOUT) section I swapped printmessage("TIMEOUT!"); for:</p> <pre><code>while (PeekMessage(&amp;msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&amp;msg); DispatchMessage(&amp;msg); } </code></pre> <p>I'm not closing this question yet because I'd still like to know why the original code did not work all by itself.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload